【发布时间】:2020-07-07 12:30:59
【问题描述】:
以下是我的数据
df <- data.frame(Lab = c("Queen II", "MMH", "Berea", "Maluti", "Motebang"),
Expected = c(13200, 5280, 5280, 2640, 5280),
Actual = c(8759, 761, 2263, 2210, 6100),
utili_pct = c(66.35, 14.41, 42.86, 83.71, 115.53))
我试图绘制一个在图表上包含一条线的条形聊天。
第一步
# I Converted numeric variable "Actual" to a factor
df$Actualx <- as.factor(df$Actual)
这样我就可以绘制一个包含两个因素变量与一个数字变量的图表 所以我整理数据并以这种方式运行绘图,但轴刻度变得没有顺序。
tidy_Data = df %>% gather(key, value, Actualx, Expected)
ggplot(tidy_Data, aes(x=Lab, y=value, fill=key)) +
geom_bar(stat = "identity", position = position_dodge(0.8)) `
此外,
我试图添加一条线 utili_ptc 和第二个轴,但比例让我很难过,
该线与条不对齐。
ggplot(tidy_Data, aes(x=Lab, y=value, fill=key)) +
geom_bar(stat = "identity", position = position_dodge(0.8)) +
geom_line(aes(x=Lab, y=utili_pct), color = "green", group = 1)
【问题讨论】: