与其他图(ggplot2)结合时如何将 geom_bar 中的计数更改为百分比?
How to change count to percentage in geom_bar when combining with other plot (ggplot2)?
我正在尝试使用 ggplot2 将两个图形合二为一。我有两个共享一个公共变量(因子)的数据框。它们看起来像这样:
tb1:
study caribbean south.america
alison_2010 1 0
james_1998 0 1
...
tb2:
study stage.I stage.II stage III ...
alison_2010 95.6 93.1 81.3
james_1998 94.2 80.7 74.5
...
我想用这两种信息绘制一张图(结果如 tb2
所示,起源区域如 tb1
所示)。
Tb1
将绘制为条形图(以在背景上创建 retangles),tb2
将绘制为点图。
我试过这个:
tb1<- melt(tb1, id.vars="study")
tb2<- melt(tb2, id.vars="study")
c<-ggplot()+
geom_bar(data=tb1, aes(y=tb1$value, x=tb1$study), fill=tb1$variable),
stat="identity", position_fill(reverse = TRUE))+
geom_dotplot(data=tb2, aes(x=tb2$study, y=tb2$value, color=tb2$variable, fill=tb2$variable),
binaxis="y", stackdir="center", binwidth=1, dotsize=1.5, group=1)
我明白了:
当我加上+ scale_y_continuous(labels=scales::percent)
我明白了:
我尝试在 tb1
上使用 100 insted of 1,或者将 tb2
中的值除以 100。没用;/
我现在不担心标签或任何东西。我只想让条形图绘制百分比,而不是计数。谁能帮我?谢谢!
您想要在您的点后面绘制箱线图,但您的箱子都是零或一,这似乎很不寻常。我采取了两种方法来回答你的问题。
首先设置数据:
tb1 = data.frame(study = c("alison_2010", "james_1998"),
caribbean = c(1,0),
south.america = c(0,1))
tb2 = data.frame(study = c("alison_2010", "james_1998"),
stage1 = c(95,94),
stage2 = c(93,80),
stage3 = c(81,74))
tb1a = melt(tb1, id.vars = "study")
tb2a = melt(tb2, id.vars = "study")
tba = inner_join(tb1a, tb2a, by = "study") %>% filter(value.x == 1)
方法 1:假设您只想绘制加勒比海的 Alison 和南美的 James,后面的条形图:
ggplot(data = tba) +
geom_col(aes(x=interaction(study,variable.y), y=value.x, fill=variable.x)) +
geom_point(aes(x=interaction(study,variable.y), y=value.y/100, color = variable.y), size = 5) +
scale_colour_manual(values = c("purple","orange","grey"))
备注:
如果您还想在 Caribbean = 0 时绘制地块,则需要删除过滤器。
条形图使用'fill',点使用'color',否则很难将两者分开颜色。
方法 2:预计分面将是您问题的更简单解决方案:
ggplot(data=tba) +
geom_point(aes(x=study, y=value.y, color = variable.y), size = 5) +
facet_grid(.~variable.x)
我正在尝试使用 ggplot2 将两个图形合二为一。我有两个共享一个公共变量(因子)的数据框。它们看起来像这样:
tb1:
study caribbean south.america
alison_2010 1 0
james_1998 0 1
...
tb2:
study stage.I stage.II stage III ...
alison_2010 95.6 93.1 81.3
james_1998 94.2 80.7 74.5
...
我想用这两种信息绘制一张图(结果如 tb2
所示,起源区域如 tb1
所示)。
Tb1
将绘制为条形图(以在背景上创建 retangles),tb2
将绘制为点图。
我试过这个:
tb1<- melt(tb1, id.vars="study")
tb2<- melt(tb2, id.vars="study")
c<-ggplot()+
geom_bar(data=tb1, aes(y=tb1$value, x=tb1$study), fill=tb1$variable),
stat="identity", position_fill(reverse = TRUE))+
geom_dotplot(data=tb2, aes(x=tb2$study, y=tb2$value, color=tb2$variable, fill=tb2$variable),
binaxis="y", stackdir="center", binwidth=1, dotsize=1.5, group=1)
我明白了:
当我加上+ scale_y_continuous(labels=scales::percent)
我明白了:
我尝试在 tb1
上使用 100 insted of 1,或者将 tb2
中的值除以 100。没用;/
我现在不担心标签或任何东西。我只想让条形图绘制百分比,而不是计数。谁能帮我?谢谢!
您想要在您的点后面绘制箱线图,但您的箱子都是零或一,这似乎很不寻常。我采取了两种方法来回答你的问题。
首先设置数据:
tb1 = data.frame(study = c("alison_2010", "james_1998"),
caribbean = c(1,0),
south.america = c(0,1))
tb2 = data.frame(study = c("alison_2010", "james_1998"),
stage1 = c(95,94),
stage2 = c(93,80),
stage3 = c(81,74))
tb1a = melt(tb1, id.vars = "study")
tb2a = melt(tb2, id.vars = "study")
tba = inner_join(tb1a, tb2a, by = "study") %>% filter(value.x == 1)
方法 1:假设您只想绘制加勒比海的 Alison 和南美的 James,后面的条形图:
ggplot(data = tba) +
geom_col(aes(x=interaction(study,variable.y), y=value.x, fill=variable.x)) +
geom_point(aes(x=interaction(study,variable.y), y=value.y/100, color = variable.y), size = 5) +
scale_colour_manual(values = c("purple","orange","grey"))
备注:
如果您还想在 Caribbean = 0 时绘制地块,则需要删除过滤器。
条形图使用'fill',点使用'color',否则很难将两者分开颜色。
方法 2:预计分面将是您问题的更简单解决方案:
ggplot(data=tba) +
geom_point(aes(x=study, y=value.y, color = variable.y), size = 5) +
facet_grid(.~variable.x)