【问题标题】:Beside Barchart with ggplot()-function is not working in R-studio带有 ggplot() 函数的条形图在 R-studio 中不起作用
【发布时间】:2020-07-19 14:00:14
【问题描述】:

我正在尝试为 x 轴上的一个类别创建一个包含两个或多个值(条形)的条形图。但是,它不适用于 ggplot() 函数。它没有显示并排的条形图。我认为价值观是重叠的。但是,我把位置设为 ="dodge"。

我有两个数据集,一个用于 2020 年,一个用于 2019 年,具有相同的类别,我想将它们一起绘制在一个条形图中,每个类别有两个条形图。

我使用了以下代码: tgc_combi = rbind(tgc20, tgc19) #首先,我将两个数据集结合起来,然后尝试绘制它们:

ggplot(tgc_combi, aes(x=Category, y=Visitors)) + 
  ggtitle("Number of visitors in each category")+xlab("Category")+ylab("Visitor numbers") +
  theme(plot.title = element_text(hjust = 0.5))+
  geom_bar(position="dodge", stat="identity") +
  geom_errorbar(aes(ymin=Visitors-se, ymax=Visitors+se),
                width=.2,                   
                position=position_dodge(.9))

可能是因为 tgc20 和 tgc19 中的类别名称相同? 谁能帮帮我?

【问题讨论】:

  • 您需要设置aes(fill = [some_column])group = [some_column]才能使条形分组生效。
  • 谢谢@jdobres。我都试过了,但是,它仍然覆盖了我的栏。我有 8 个类别,所以它应该绘制 16 个条,但是,它只绘制 8 个条,每个条中有两个误差条。还有其他想法吗?我是否可能需要以不同的方式合并我的两个数据集(tgc20 和 tgc19)?
  • 请提供一个可重现的例子。
  • tgc20 和 tgc19 看起来像这样:N 类访客 1 行为 83 2 出生 27 3 缺点 16 4 EDU 19 5 GE 106 6 HEALTH 15 7 NEW 4 8 OUT 2 当我与 rbind() 合并时,它看起来像这样: N 类访客 1 BEHAV 83 2 BIRTH 27 3 CONS 16 4 EDU 19 5 GE 106 6 HEALTH 15 7 NEW 4 8 OUT 12 9 BEHAV 53 10 BIRTH 13 11 CONS 3 12 EDU 4 13 GE 39 14 HEALTH 7 15 新 3 16 出 11

标签: r ggplot2 bar-chart


【解决方案1】:

您的aes 中缺少group。如果没有group,则无法区分 2019 年和 2020 年的数据。此外,如果您有较浅的颜色条,您会注意到较低值的阴影较深,在您的示例中恰好是 2020 年的值。 我假设您已经计算了se,并将它们放在您的数据框中。

以下代码

c <- c("BEHAV", "BIRTH", "CONS", "EDU", "GE", "HEALTH", "NEW", "OUT")
v <- c(83, 27, 16, 19, 106, 15, 4, 12)
se1 <- c(8.8,3.3,0.9,2.1,5.6,1.1,0.5,2.8)
y <- c(rep(2019,8))               
tgc19 <- data.frame(Category=c, Visitors=v, Year=y, se=se1)
v2 <- c(53, 13, 3, 4, 39, 7, 3, 11)
se2 <- c(9.8,2.3,1.9,1.5,4.6,0.6,1.1,2.2)
y2 <- c(rep(2020,8))               
tgc20 <- data.frame(Category=c, Visitors=v2, Year=y2, se=se2)

tgc_combi <- rbind(tgc20,tgc19)
tgc_combi$Category <- factor(tgc_combi$Category, levels=c)
dodge <- position_dodge(width = 0.9)
limits <- aes(ymax = Visitors + se,
              ymin = Visitors - se)

ggplot(tgc_combi, aes(x=Category, y=Visitors, group=Year, fill=Year, color=Year)) + 
  labs( title="Number of visitors in each category", x = "Category", y= "Visitor numbers") +
  geom_bar(stat="identity", position="dodge") +
  scale_x_discrete(labels=unique(tgc_combi$Category)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  geom_errorbar(limits, position = dodge, width = 0.2, color=c("red")) 

给出以下输出:

【讨论】:

  • 谢谢@YBS!如何将误差条添加到这两年)?我使用了这个代码:ggplot(tgc_combi, aes(x=Category, y=Visitors, group=Year, fill=Year, color=Year)) + labs( title="Number of visitors in each category", x = "Category", y= "Visitor numbers") + theme(plot.title = element_text(hjust = 0.5)) + geom_bar(stat="identity", position="dodge") + scale_x_discrete(labels=unique(tgc_combi$Category)+ geom_errorbar(aes(ymin=c(v$Engagement-se,v2$Engagement-se), ymax=c(v$Engagement+se,v2$Engagement+se), width=.2, position=position_dodge(.9)))
  • 请查看更新后的代码和输出。您的代码看起来应该可以工作。
猜你喜欢
  • 1970-01-01
  • 2021-11-10
  • 2022-09-30
  • 2013-06-02
  • 2020-08-10
  • 2022-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多