ggplot:如何将组平均值作为线添加到 R 中的分组条形图?
ggplot: How to add a group average value as a line to a grouped bar chart in R?
我想将 "All_Parties_average" 值作为带有图例的绿线添加到我的条形图中(参见 link 添加的第二张图片)。非常感谢您的帮助!
现在我的图表如下所示:
My graph at the moment
这就是我想要的样子:
End result
这是我的数据:
Gender <- as.factor(c("M","M","M","M","M","M","M","M","M","M","F","F","F","F","F","F","F","F","F","F"))
Political_Party <- as.factor(c("E200", "EKRE", "ERE", "Isamaa", "REF","Roh", "SDE", "Vabae", "Vasak", "KE","E200", "EKRE", "ERE", "Isamaa", "REF","Roh", "SDE", "Vabae", "Vasak", "KE"))
Media_coverage_average <- c(61,95,41,162,107,23,130,68,6,152,99,49,9,87,157,52,112,31,14,202)
All_Parties_average <- c(90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90)
my_data <- data.frame(Sugu, Political_Party, Media_coverage_average, All_Parties_average, stringsAsFactors=F)
还有图表的颜色:
my_colors <- c(
`Green` <- "#00a99d",
`Purple` <- "#9424fb",
`Grey` <- "#a6a6a6",
`LightPurple` <- "#c991fd",
`LightGrey` <- "#c9c9c9",
`DarkGrey`<- "#636363",
`ExtraDarkGrey`<- "#4c4c4c")
我的图表:
my_plot <- ggplot(my_data, aes(x = Political_Party, y = Media_coverage_average, fill = Gender))+
geom_bar(position="dodge", stat="identity")+
geom_text(aes(label=Media_coverage_average), position=position_dodge(width=0.9), vjust=-0.25, size=6, family="Titillium Web", color= ExtraDarkGrey)+
labs(x="Political Party", y="Average media coverage",
caption = "Author")+
theme_ipsum_tw(grid=FALSE, axis_text_size=18, axis_title_size = 16, caption_size = 15, base_size = 17, subtitle_size = 20)+
scale_fill_manual(values = c(Grey, Purple), labels = c("Mehed", "Naised"))
my_plot
也许你可以用 geom_line
添加
my_plot <- ggplot(my_data, aes(x = Political_Party, y = Media_coverage_average, fill = Gender))+
geom_bar(position="dodge", stat="identity")+
geom_text(aes(label=Media_coverage_average), position=position_dodge(width=0.9), vjust=-0.25, size=6, family="Titillium Web", color= ExtraDarkGrey)+
labs(x="Political Party", y="Average media coverage",
caption = "Author")+
scale_fill_manual(values = c(Grey, Purple), labels = c("Mehed", "Naised"))+
theme_ipsum_tw(grid=FALSE, axis_text_size=18, axis_title_size = 16, caption_size = 15, base_size = 17, subtitle_size = 20)+
geom_line(aes(y=All_Parties_average,group=1),size=1.5,color="green")
除@Senzeybek 的回答外,您还可以通过将其颜色传递到 geom_line
的 aes
并使用 scale_color_manual
设置颜色来为绿线添加图例:
library(ggplot2)
ggplot(my_data, aes(x = Political_Party, y = Media_coverage_average, fill = Gender))+
geom_bar(position="dodge", stat="identity")+
geom_text(aes(label=Media_coverage_average), position=position_dodge(width=0.9), vjust=-0.25, size=6, family="Titillium Web", color= ExtraDarkGrey)+
labs(x="Political Party", y="Average media coverage",
caption = "Author")+
#theme_ipsum_tw(grid=FALSE, axis_text_size=18, axis_title_size = 16, caption_size = 15, base_size = 17, subtitle_size = 20)+
scale_fill_manual(values = c(Grey, Purple), labels = c("Mehed", "Naised"))+
geom_hline(aes(yintercept = All_Parties_average, color = "Average", group = 1), size = 2)+
scale_color_manual(values = "green")
我想将 "All_Parties_average" 值作为带有图例的绿线添加到我的条形图中(参见 link 添加的第二张图片)。非常感谢您的帮助!
现在我的图表如下所示:
My graph at the moment
这就是我想要的样子:
End result
这是我的数据:
Gender <- as.factor(c("M","M","M","M","M","M","M","M","M","M","F","F","F","F","F","F","F","F","F","F"))
Political_Party <- as.factor(c("E200", "EKRE", "ERE", "Isamaa", "REF","Roh", "SDE", "Vabae", "Vasak", "KE","E200", "EKRE", "ERE", "Isamaa", "REF","Roh", "SDE", "Vabae", "Vasak", "KE"))
Media_coverage_average <- c(61,95,41,162,107,23,130,68,6,152,99,49,9,87,157,52,112,31,14,202)
All_Parties_average <- c(90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90)
my_data <- data.frame(Sugu, Political_Party, Media_coverage_average, All_Parties_average, stringsAsFactors=F)
还有图表的颜色:
my_colors <- c(
`Green` <- "#00a99d",
`Purple` <- "#9424fb",
`Grey` <- "#a6a6a6",
`LightPurple` <- "#c991fd",
`LightGrey` <- "#c9c9c9",
`DarkGrey`<- "#636363",
`ExtraDarkGrey`<- "#4c4c4c")
我的图表:
my_plot <- ggplot(my_data, aes(x = Political_Party, y = Media_coverage_average, fill = Gender))+
geom_bar(position="dodge", stat="identity")+
geom_text(aes(label=Media_coverage_average), position=position_dodge(width=0.9), vjust=-0.25, size=6, family="Titillium Web", color= ExtraDarkGrey)+
labs(x="Political Party", y="Average media coverage",
caption = "Author")+
theme_ipsum_tw(grid=FALSE, axis_text_size=18, axis_title_size = 16, caption_size = 15, base_size = 17, subtitle_size = 20)+
scale_fill_manual(values = c(Grey, Purple), labels = c("Mehed", "Naised"))
my_plot
也许你可以用 geom_line
添加my_plot <- ggplot(my_data, aes(x = Political_Party, y = Media_coverage_average, fill = Gender))+
geom_bar(position="dodge", stat="identity")+
geom_text(aes(label=Media_coverage_average), position=position_dodge(width=0.9), vjust=-0.25, size=6, family="Titillium Web", color= ExtraDarkGrey)+
labs(x="Political Party", y="Average media coverage",
caption = "Author")+
scale_fill_manual(values = c(Grey, Purple), labels = c("Mehed", "Naised"))+
theme_ipsum_tw(grid=FALSE, axis_text_size=18, axis_title_size = 16, caption_size = 15, base_size = 17, subtitle_size = 20)+
geom_line(aes(y=All_Parties_average,group=1),size=1.5,color="green")
除@Senzeybek 的回答外,您还可以通过将其颜色传递到 geom_line
的 aes
并使用 scale_color_manual
设置颜色来为绿线添加图例:
library(ggplot2)
ggplot(my_data, aes(x = Political_Party, y = Media_coverage_average, fill = Gender))+
geom_bar(position="dodge", stat="identity")+
geom_text(aes(label=Media_coverage_average), position=position_dodge(width=0.9), vjust=-0.25, size=6, family="Titillium Web", color= ExtraDarkGrey)+
labs(x="Political Party", y="Average media coverage",
caption = "Author")+
#theme_ipsum_tw(grid=FALSE, axis_text_size=18, axis_title_size = 16, caption_size = 15, base_size = 17, subtitle_size = 20)+
scale_fill_manual(values = c(Grey, Purple), labels = c("Mehed", "Naised"))+
geom_hline(aes(yintercept = All_Parties_average, color = "Average", group = 1), size = 2)+
scale_color_manual(values = "green")