我将 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))

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

要获得我们可以做的标签
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/

10-12 00:28
查看更多