r - 使用 ggplot 组合条形图和折线图

标签 r ggplot2

我将 2 个系列绘制为条形图,将一个系列绘制为带有 ggplot 的线,但折线图不知何故不可见。无法弄清楚错误。而且我想为条形和线条添加标签。

dput(data_plot)

structure(list(career_level = structure(c(5L, 2L, 1L, 4L, 3L, 
8L, 9L, 7L, 6L, 5L, 2L, 1L, 4L, 3L, 8L, 9L, 7L, 6L), .Label = c("Executives", 
"Head of Organization", "Management-NonSales", "Management-sales", 
"Overall", "Para-Professional- Blue Collar", "Para professional -white collar", 
"Professional- Sales", "Professional-Non Sales"), class = "factor"), 
    variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("actual_2018", 
    "budget_2019"), class = "factor"), value = c(5.3, 5.1, 5, 
    3.3, 5.3, 2.2, 2.3, 2.2, 2.2, 5.3, 5.1, 5, 3.3, 5.3, 2.2, 
    2.3, 2.2, 2.2)), row.names = c(NA, -18L), class = "data.frame")

dput(forecast)
structure(list(career_level = structure(c(5L, 2L, 1L, 4L, 3L, 
8L, 9L, 7L, 6L), .Label = c("Executives", "Head of Organization", 
"Management-NonSales", "Management-sales", "Overall", "Para-Professional- Blue Collar", 
"Para professional -white collar", "Professional- Sales", "Professional-Non Sales"
), class = "factor"), variable = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L), .Label = "forecast_2020", class = "factor"), 
    value = c(5.3, 5.1, 5, 3.3, 5.3, 2.2, 2.3, 2.2, 2.2)), row.names = c(NA, 
-9L), class = "data.frame")



ggplot()+
  geom_bar(data=data_plot,aes(x=career_level,y=value,fill=variable),stat="identity",position = "dodge")+
  geom_point(data=forecast,aes(x=career_level,y=value))+
  geom_line(data=forecast,aes(x=career_level,y=value,colour=variable))

我希望预测是图表上的一条线。

此外,我正在尝试使用此代码作为标签,但它在条形上给出了组合标签,而不是单独的标签,我也希望在条形和线条上都有单独的标签:
ggplot()+
  geom_bar(data=data_plot,aes(x=career_level,y=value,fill=variable),stat="identity",position = "dodge")+
    geom_text(data=data_plot, aes(x=career_level,y=value,label = paste0(value, "%")),face= "bold",color="#FDFEFE",size=3,position = position_stack(vjust = 0.5))+
  geom_point(data=forecast,aes(x=career_level,y=value))+
  geom_line(data=forecast,aes(x=career_level,y=value,colour=variable,group=1))

最佳答案

geom_line 中添加分组美学

library(ggplot2)

ggplot() +
geom_bar(data=data_plot, aes(x=career_level,y=value,fill=variable),
          stat="identity",position = "dodge")
geom_point(data=forecast,aes(x=career_level,y=value)) +
geom_line(data=forecast,aes(x=career_level,y = value,colour=variable, group = 1))

enter image description here

要获得我们可以做的标签
ggplot(data_plot) +
   aes(x=career_level,y=value,fill=variable) +
   geom_bar(,stat="identity",position = "dodge") +
   geom_text(aes(label=paste0(value, "%")), 
        position=position_dodge(width = 1), vjust=-0.5)  +
   geom_point(data=forecast,aes(x=career_level,y=value)) + 
   geom_line(data=forecast,aes(x=career_level,y=value,colour=variable,group=1)) + 
   geom_text(data = forecast, aes(x=career_level,y=value,label=paste0(value, "%")), 
          position=position_dodge(width =1), vjust= -2.5)

您可能需要调整标签的高度和宽度以使标签可见。

关于r - 使用 ggplot 组合条形图和折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56285870/

相关文章:

linux - ggplot2 的直方图需要一个连续的 x 变量

r - 在 ggplot2 中微调 stat_ellipse()

r - ggtext 使用什么格式化语言来格式化文本?

r - 在 ggplot2 中的密度图上应用渐变填充

r - 如何光栅化ggplot的单层?

r - ggplot2 增加图例键之间的空间

使用 sqldf 检索列的具有相同最小值的所有行

r - 加速计算每列 3 元组的行中位数

r - 首次在 r 中的预测包中计算预测时,请选择更长的范围

r - 在 R 统计中的 ggplot2 中标记每个箱线图中两条 mustache 的末端