【问题标题】:How to shade a region under a horizontal line transparently using ggplot2?如何使用ggplot2透明地对水平线下的区域进行着色?
【发布时间】:2015-10-03 00:13:13
【问题描述】:

这是原图。

这是我用来生成上图的代码。

## Employees Wise Sales Report: MAY 2014-LYNDA Best Visualization Assignment
setwd('d:/dataset/lynda')
empwisedata=read.csv('income.csv',header=T,sep=",")
names(empwisedata)
attach(empwisedata)

Minimum=c(min(Person1),min(Person2),min(Person3),min(Person4),min(Person5))
Average=c(mean(Person1),mean(Person2),mean(Person3),mean(Person4),mean(Person5))
Maximum=c(max(Person1),max(Person2),max(Person3),max(Person4),max(Person5))
attach(Average)

library(ggplot2)
library(reshape2)
df = melt(data.frame(Minimum,Average,Maximum,Employees=c("Person1", "Person2","Person3","Person4","Person5")),variable.name="IncomeLevels")
df$Employees<-factor(df$Employees,levels = df$Employees[order(Average)])

p=ggplot(df, aes(Employees, value, fill=IncomeLevels)) +   geom_bar(position="dodge",stat="identity")

p + geom_hline(yintercept=mean(Average))+scale_fill_manual(values=c("red","orange","dark green"))+labs(size= "Nitrogen", x = "Employees of ACME Widgets",y = "Income in USD", title = "ACME WIDGETS :: Employees Wise Sales Report-MAY 2014 ")

我想在图表中的水平线下方填充颜色。我已经通过修改上面代码的最后一行来尝试 geom_rect,如下所示。

p+geom_hline(yintercept=mean(Average)) + scale_fill_manual(values=c("red","orange","dark green"))+labs(size= "Nitrogen", x = "Employees of ACME Widgets",y = "Income in USD", title = "ACME WIDGETS :: Employees Wise Sales Report-MAY 2014 ") + geom_rect(xmin=0,xmax=200,ymin=0,ymax=mean(Average),fill="blue")

并得到以下图像。

我不需要深蓝色。我需要透明度,以便还可以查看平均条(黄色)。我也尝试过不同的阿尔法水平。但没有任何效果。感谢您的帮助。

解决方案: 我根据 LukeA 的建议修改了代码的最后一行。代码是

p+geom_hline(yintercept=mean(Average))+scale_fill_manual(values=c("red","orange","dark green"))+labs(size= "Nitrogen", x = "Employees of ACME Widgets",y = "Income in USD", title = "ACME WIDGETS :: Employees Wise Sales Report-MAY 2014 ")+  annotate("rect", xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = mean(Average), fill = "blue", alpha = .1, color = NA)

并得到如下所述的所需输出图。

谢谢大家。

【问题讨论】:

    标签: r ggplot2 horizontal-line


    【解决方案1】:

    试试这样的

    library(ggplot2)
    ggplot(mtcars, aes(mpg)) + 
      geom_histogram() + 
      annotate("rect", xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = 1, fill = "blue", alpha = .5, color = NA)
    

    【讨论】:

    • @VeeramaniNatarajan 如果您发现 lukeA 的回答最有帮助(位于他们回答的左上角),您可能需要检查绿色复选标记。
    • 有谁知道如何将图例添加到位于 x 轴和深色水平线之间的矩形条
    • 在这种情况下,您必须使用美学映射。例如。 ggplot() + geom_histogram(data = mtcars, aes(mpg)) + geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = 1, fill = "fill"), alpha = .5, color = NA) + scale_fill_manual(values = "blue") + labs(fill = "Legend").
    • 如果 x 轴是日期时间,并且我想要一个类似的半透明框用于任何时间范围,是否有替代 Inf 或其他简单方法?我得到Error: Invalid input: time_trans works with objects of class POSIXct only
    • @doconnor library(ggplot2) o &lt;- "1970-01-01"; ggplot(mtcars, aes(as.POSIXct(mpg, orig = o))) + geom_histogram() + annotate("rect", xmin = as.POSIXct(-Inf, orig = o), xmax = as.POSIXct(Inf, orig = o), ymin = -Inf, ymax = 1, fill = "blue", alpha = .5, color = NA)?
    猜你喜欢
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多