更改alpha不会影响ggplot2中的任何内容

时间:2012-11-08 02:49:36

标签: r graph ggplot2 alpha

我对R和ggplot2有些新意,所以这个问题可能有些低级。但是我做了大量的实验并且没有在网上找到答案,所以我想我会在这里问。

当我 alpha添加到我的图表时,图表显示如下:

Some alpha

但是,无论我如何更改 alpha的值,我都无法在图表中进行任何更改。我尝试了alpha = .9和alpha = 1/10000,图表中没有任何差异。

然而似乎'alpha'一词正在做某事。当我从代码中删除'alpha'时,我得到以下图表:

No alpha

这是我的代码。谢谢!

library(ggplot2)
library(chron)
argv <- commandArgs(trailingOnly = TRUE)
mydata = read.csv(argv[1])
png(argv[2], height=300, width=470)


timeHMS_formatter <- function(x) {                  # Takes time in seconds from midnight, converts to HH:MM:SS
h <- floor(x/3600)
m <- floor(x %% 60)
s <- round(60*(x %% 1))                         # Round to nearest second
lab <- sprintf('%02d:%02d', h, m, s)        # Format the strings as HH:MM:SS
lab <- gsub('^00:', '', lab)                    # Remove leading 00: if present
lab <- gsub('^0', '', lab)                      # Remove leading 0 if present
}

dateEPOCH_formatter <- function (y){
epoch <- c(month=1,day=1,year=1970)
    chron(floor(y),out.format="mon-year",origin.=epoch)
}

p=  ggplot() + 
coord_cartesian(xlim=c(min(mydata$day),max(mydata$day)), ylim=c(0,86400)) +         # displays data from first email through present
scale_color_hue() +
xlab("Date") +
ylab("Time of Day") +
scale_y_continuous(label=timeHMS_formatter, breaks=seq(0, 86400, 7200)) +           # adds tick marks every 2 hours
scale_x_continuous(label=dateEPOCH_formatter, breaks=seq(min(mydata$day), max(mydata$day), 365) ) +
ggtitle("Email Sending Times") +                                                        # adds graph title
theme( legend.position = "none", axis.title.x = element_text(vjust=-0.3)) +
layer(
    data=mydata, 
    mapping=aes(x=mydata$day, y=mydata$seconds, alpha=1/2, size=5), 
    stat="identity", 
    stat_params=list(), 
    geom="point", 
    geom_params=list(),
    position=position_identity(),
)       

print(p)
dev.off()

1 个答案:

答案 0 :(得分:4)

您需要将alpha规范放在<{1}}语句之外,如

mapping

我更习惯于表达这一点,因为

layer(
    data=mydata, 
    mapping=aes(x=day, y=seconds), 
    stat="identity", 
    stat_params=list(), 
    geom="point", 
    geom_params=list(alpha=1/2, size=5),
    position=position_identity(),
)       

其他被排除的东西代表默认值,我相信......

另请参阅:Why does the ggplot legend show the "colour" parameter?