【问题标题】:How to show both count and proportion in the same plot for two periods in GGplot2?如何在GGplot2中的两个时期的同一图中同时显示计数和比例?
【发布时间】:2020-06-10 08:05:41
【问题描述】:

我是一个新手,在同一个情节中努力呈现频率和比例。我能够制作一个显示频率的两个周期的网格。我想显示每种水果(和狗)的比例以提供更多信息。我设法画了一条线,但它非常难看......我尝试了密度并且惨遭失败。

此外,我无法将 gg1(左图)的轴固定在 10 的比例上。(我解决了这个问题;更新 2)

current plot

library(tidyverse)
library(magrittr)
library(dplyr)
library(ggplot2)
library(grid)
library(gridExtra)

policy <- data.frame("names" = c("Apple", "Banana", "Mango", "Dog"),
                    "period1" = c(1,7,2,2),
                    "period2" = c(4,10,4,2))

#create new column with "names" that breaks into new line. for nicer plot
names2 <- as.character(gsub(" ", "\n", policy$names))
policy$names <- names2

#manage font size in ggplot for names of bars, and in theme for title
gg.mid<-ggplot(policy,aes(x=1,y=names))+geom_text(aes(label=names2), size=4.5)+
 ggtitle("Policy Types")+
 ylab(NULL)+
 scale_x_continuous(expand=c(0,0),limits=c(1.0,1.0))+
 theme(plot.title = element_text(hjust = 0.5, size = 15),
       axis.title=element_blank(),
       panel.grid=element_blank(),
       axis.text.y=element_blank(),
       axis.ticks.y=element_blank(),
       panel.background=element_blank(),
       axis.text.x=element_text(color=NA),
       axis.ticks.x=element_line(color=NA))

gg1 <- ggplot(data = policy, aes(x = names, y = period1)) +
 geom_bar(stat = "identity") + ggtitle("Pre-2012") +
 theme(plot.title = element_text(hjust = 0.5, size = 15),
       axis.title.x = element_blank(), 
       axis.title.y = element_blank(), 
       axis.text.y = element_blank(), 
       axis.ticks.y = element_blank()) +
 scale_y_reverse() + coord_flip(ylim = c(10,0))

gg2 <- ggplot(data = policy, aes(x = names, y = period2))+
 geom_bar(stat = "identity") + ggtitle("Post-2012") +
 theme(plot.title = element_text(hjust = 0.5, size = 15),
       axis.title.x = element_blank(),
       axis.title.y = element_blank(), 
       axis.text.y = element_blank(),
       axis.ticks.y = element_blank()) +
 scale_y_continuous(expand = c(0, 0), limits = c(0, NA)) + #this make Y axis (reverse) strats at 0 and stops at biggest value)
 coord_flip()



grid.arrange(gg1, gg.mid, gg2, ncol=3, widths=c(4/9,2.3/9,4/9))

提前谢谢...

更新

我尝试将 ylim(0, 10) 添加到 gg1(左图),但得到了这个...

enter image description here

更新 2

将 expand = c(0, 0) 添加到 coord_flip() 中修复它。

gg1 <- ggplot(data = policy, aes(x = names, y = period1)) +
  geom_bar(stat = "identity") + ggtitle("Pre-2012") +
  theme(plot.title = element_text(hjust = 0.5, size = 15),
        axis.title.x = element_blank(), 
        axis.title.y = element_blank(), 
        axis.text.y = element_blank(), 
        axis.ticks.y = element_blank()) +
  scale_y_reverse() + coord_flip(ylim = c(10,0), expand = c(0, 0))

【问题讨论】:

  • “proprtion”是指“每个元素的总数对列总数的贡献”吗?那么对于 period1 来说它将是 period1/sum(period1)?至于你的轴范围问题,你试过xlim(0, 10)吗?还是coord_cartesian()
  • 是的,你是对的。按比例,我的意思是 e.i.特定时期内所有水果中苹果的百分比。关于限制,我尝试了 ylim(它被翻转了),但随后情节发生了奇怪的变化。
  • 好的。在这种情况下,coord_flip(xlim(0, 10)) 可能会给你你想要的。我会尽可能写一个正确的答案。
  • 解决了轴范围问题(更新 2)。我仍然不知道如何可视化比例。

标签: r ggplot2 plot


【解决方案1】:

这里的解决方案似乎可以满足您的需求:

gg1 <- ggplot(data = policy, aes(x = names, y = period1)) +
  geom_bar(stat = "identity") + ggtitle("Pre-2012") +
  theme(plot.title = element_text(hjust = 0.5, size = 15),
        axis.title.x = element_blank(), 
        axis.title.y = element_blank(), 
        axis.text.y = element_blank(), 
        axis.ticks.y = element_blank()) +
  scale_y_reverse(sec.axis=sec_axis(name="Proportion", trans=~./sum(policy$period1))) + 
  coord_flip(ylim = c(10,0))

您的expand=c(0,0) 会生成警告,似乎没有必要。

【讨论】:

  • 我确信有一种更简洁的方式来指定转换。这不是我的强项。如果有人有更好的建议,请随意!
猜你喜欢
  • 1970-01-01
  • 2018-11-25
  • 1970-01-01
  • 2020-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-25
  • 1970-01-01
相关资源
最近更新 更多