相关疑难解决方法(0)

如何使用ggplot2上的标识控制堆积条形图的排序

使用这个假人 data.frame

ts <- data.frame(x=1:3, y=c("blue", "white", "white"), z=c("one", "one", "two"))
Run Code Online (Sandbox Code Playgroud)

我尝试用顶部的"蓝色"类别绘图.

ggplot(ts, aes(z, x, fill=factor(y, levels=c("blue","white" )))) + geom_bar(stat = "identity")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

给我"白色"在上面.和

ggplot(ts, aes(z, x, fill=factor(y, levels=c("white", "blue")))) + geom_bar(stat = "identity")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

颠倒颜色,但仍然让我在上面"白色".我怎样才能获得"蓝色"?

r ggplot2

33
推荐指数
5
解决办法
6万
查看次数

Barplot酒吧走向错误的方向

我是一名学习R的生物学研究生.我希望有人可以帮助我让水平向相反的方向移动(蓝色部分应该从0开始,红色在100的末端).

条形图与错误的方向

在此输入图像描述

这是数据

my_species <- c('apomict_2-17-17_compreh', 'apomict_2-17-17_compreh', 'apomict_2-17-17_compreh', 'apomict_2-17-17_compreh', 'parthenogen_2-17-17_compreh', 'parthenogen_2-17-17_compreh', 'parthenogen_2-17-17_compreh', 'parthenogen_2-17-17_compreh', 'sexual_2-9-17', 'sexual_2-9-17', 'sexual_2-9-17', 'sexual_2-9-17')
my_species <- factor(my_species)
my_species <- factor(my_species,levels(my_species)[c(length(levels(my_species)):1)]) # reorder your species here just by changing the values in the vector :
my_percentage <- c(36.3, 56.3, 2.6, 4.8, 42.2, 50.6, 2.4, 4.8, 56.0, 19.9, 6.7, 17.4)
my_values <- c(522, 811, 38, 69, 608, 729, 35, 68, 806, 286, 96, 252)
category <- c(rep(c("S","D","F","M"),c(1)))
category <-factor(category)
category = factor(category,levels(category)[c(4,1,2,3)])
df = data.frame(my_species,my_percentage,my_values,category)
Run Code Online (Sandbox Code Playgroud)

这是代码:

# Load the …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

3
推荐指数
1
解决办法
469
查看次数

ggplot 因子的反向轴顺序

这是基础图,从下到上按月份排序,从一到十二个月。我想从上到下订购一到十二个。

library(tidyverse)
library(nycflights13)
library(ggridges)
ggplot(weather %>% filter(temp > 50), aes(x = temp, y = as.factor(month))) + 
  geom_density_ridges()
Run Code Online (Sandbox Code Playgroud)

捕获.png

这两种解决方案都会产生错误。什么是正确的解决方案?

# BROKEN SOLUTION 1    
ggplot(weather %>% filter(temp > 50), aes(x = temp, y = as.factor(month))) + 
  geom_density_ridges() + 
  scale_y_continuous(trans = "reverse")
Run Code Online (Sandbox Code Playgroud)

错误:提供给连续刻度的离散值。另外:警告消息:1:在 Ops.factor(x) 中:“-”对因子没有意义。2:变换在连续 y 轴上引入了无限值。

并且

# BROKEN SOLUTION 2
ggplot(weather %>% filter(temp > 50), aes(x = temp, y = as.factor(month))) + 
  geom_density_ridges() + 
  scale_y_discrete(limits = rev(levels(as.factor(month))))
Run Code Online (Sandbox Code Playgroud)

is.factor(x) 中的错误:找不到对象“月”

r ggplot2 ggridges

2
推荐指数
1
解决办法
4864
查看次数

ggplot2 堆叠条,将 NA 放在顶部

这里的答案有很多关于在堆积条形图中订购条形部分的重要信息。在尝试了各种替代方案并获得了我想要的大部分订单之后,NA 不断出现在堆栈的底部,这是我不喜欢的。

ggplot(df, aes(x=time, fill=forcats::fct_rev(factor(able, levels=rev(likely))))) + 
  geom_bar() + 
  theme(axis.text.x = element_text(angle = 315, hjust = 0), 
        plot.margin = margin(10, 40, 10, 10))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

x 轴上的 NA 位于末尾,这很棒。总的来说,将 NA 放在最后可能很好。但是对于堆叠的条形图,我认为开始是底部,结束是顶部(因为底部的东西更容易比较。)

(Marimekko 图表可能会更好,但我在尝试让 ggmosaic 和其他各种东西工作一段时间后放弃了。)

编辑:我发现我修改了一些代码来制作 Marimekko 图表(想给予信任,但忘记了我在哪里找到它。)它确实将 NA 放在顶部。

在此处输入图片说明

df %>%
group_by(satisfied, time) %>%
summarise(n = n()) %>%
mutate(x.width = sum(n)) %>%

ggplot(aes(x=satisfied, y=n)) +
geom_col(aes(width=x.width, fill=time),
         colour = "white", size=2, position=position_fill(reverse = T)) +
geom_text(aes(label=n),
           position=position_fill(vjust = 0.5)) +
facet_grid(~ satisfied, space = 'free', scales='free', switch='x') +
#scale_x_discrete(name="a") …
Run Code Online (Sandbox Code Playgroud)

r bar-chart ggplot2 stacked-chart

2
推荐指数
1
解决办法
941
查看次数

标签 统计

ggplot2 ×4

r ×4

bar-chart ×1

ggridges ×1

stacked-chart ×1