我在ggplot中做了一個barplot,並添加了幾行。什麼情況是,該線的顏色和說明不符合:圖例geom_hline的排列順序不正確
黃線應該有說明「中位會員」,但顯示爲「平均會員」。這裏發生了什麼?我使用的代碼:
library(ggplot2)
library(dplyr)
MemberID=c(1,1,1, 2, 2, 2)
ClientCode = c(10,100,1000, 20, 200, 2000)
Duration = c(2356, 1560, 9000, 4569, 3123, 8000)
df <- data.frame(MemberID, ClientCode, Duration)
dr <- df %>%
filter(MemberID == 1)
dr_avg <- df
ggplot(dr, aes(reorder(as.character(ClientCode), -Duration), Duration, fill=-Duration)) +
geom_bar(stat="identity") + # the height of the bar will represent the value in a column of the data frame
xlab('ClientCode') +
ylab('Duration (Minutes)') +
geom_hline(data=dr, aes(yintercept=mean(Duration), linetype = 'Avg Member'), color = 'red', show.legend = TRUE) +
geom_hline(data=dr, aes(yintercept=median(Duration), linetype = 'Median Member'), color = 'orange', show.legend = TRUE) +
geom_hline(data=dr_avg, aes(yintercept=mean(Duration), linetype = 'Avg all data'), color = 'blue', show.legend = TRUE) +
scale_linetype_manual(name = "Line", values = c(2, 2, 2), guide = guide_legend(override.aes = list(color = c("red", "orange", "blue")))) +coord_flip()
嗨阿爾弗雷德,你可能想在rstudio社區網站上發佈這個問題:https://community.rstudio.com與阿雷克斯更好地幫助他人看到你面臨的問題。 – petergensler
謝謝,不知道那個社區。 – Alfred