我正在用ggplot将2系列绘制为条形,将一个系列绘制为线,但是折线图以某种方式不可见。无法找出错误。我也想在条形和线条上添加标签。
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))
答案 0 :(得分:2)
这是通过组合两个数据集的另一种方法。这样可以节省在每个x
中键入y
和aes
的时间。
library(ggplot2)
library(scales)
combined <- rbind(data_plot, forecast)
ggplot(data = combined, aes(x = career_level, y = value, group = variable)) +
# Add bar for actual and budget
geom_bar(data = combined[combined$variable != "forecast_2020",], stat = "identity", position = "dodge", aes(fill = variable)) +
# Add point and line for forecast
geom_point(data = combined[combined$variable == "forecast_2020",]) +
geom_line(data = combined[combined$variable == "forecast_2020",]) +
# Add % labels # EDITED TO keep one decimal place and font size smaller
geom_text(aes(x = career_level, y = value, label = percent(value/100, accuracy = 0.1), group = variable),
position = position_dodge(width = 1), vjust = -0.5, size = 3)
答案 1 :(得分: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))
要获得我们可以做的标签
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)
您可能需要调整标签的高度和宽度以使标签可见。