繁体   English   中英

ggplot2 中的线下阴影

[英]Shade Under a Line in ggplot2

我正在使用 ggplot2 制作一个简单的图表。 我想做的是为图表下方的区域着色。 线条左侧一种颜色,线条右侧一种颜色。

当前代码:

ggplot(data=df, aes(x=Price, y=Number_of_Customers, group=1, color=Dmerch)) +
  geom_line(color="#FFC300", size=1)+
  labs(x="Price", y="Number of Customers", title="Willingness to Pay")+
  geom_vline(xintercept=10.84,linetype="solid",size=1, color="#FF5733")+

它制作的图表

在此处输入图像描述

我会尝试将您的 Y 变量Number_of_Customers分成两个变量y1y2 ,然后使用geom_area到 plot 用不同的 colors 分隔区域:

df <- df %>% 
  mutate(y1 = ifelse(Price>=10.84, Number_of_Customers, NA), 
         y2 = ifelse(Price<10.84, Number_of_Customers, NA))  

ggplot(data=df, aes(x=Price,group=1)) +
  geom_area(aes(y = y1), color="blue", fill= "blue", size=1)+
  geom_area(aes(y = y2), color="red", fill= "red", size=1)+
  labs(x="Price", y="Number of Customers", title="Willingness to Pay")+
  geom_vline(xintercept=10.84,linetype="solid",size=1, color="#FF5733")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM