我有不同的公司,他们可能有不同数量的相关“措施”的数据。如果某项指标低于基准值,则应该为我设定为粉红色的某种颜色着色。如果某项指标高于基准,则应该用蓝色表示。问题是,不同的公司有不同的措施数量,这些措施可能会低于或高于基准;没有模式。geom_bar的动态条件颜色ggplot
我在填充中使用这个条件,它有时会起作用。
ggplot(df, aes(measure)) + geom_col(aes(y=company, fill=overall > company)) + geom_point(aes(y=overall, color="overall"), size=8, shape=124) +
scale_color_manual("",values=c("company" = "yellow", "overall"="blue"),labels=c("company" = "Your Company", "overall"= "Overall Benchmark")) +
coord_flip()+ guides(size=FALSE) + theme(legend.box="horizontal",legend.key=element_blank(), legend.title=element_blank(),legend.position="top") +
scale_fill_manual(values=c("lightblue2", "lightpink2"),labels=c("Better","Worse"))
但是,例如,如果数据帧是这样的,它是完全关闭:
df = data.frame(
measure = c("Measure A","Measure B","Measure C","Measure D"),
overall = c(9, 5, 11, 19),
company = c(4,3,7, 16)
)
如果数据帧是这样的,它的罚款:
df2 = data.frame(
measure = c("Measure A","Measure B", "Measure C"),
overall = c(9, 5, 11),
company = c(11,7, 9)
)
我认为这种方法并不能准确地为酒吧上色,但我不确定为什么。
我没意识到你可以在ggplot中改变数据。感谢您深入解释为什么我所做的没有奏效。 – PinkyL