Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing alpha doesn't affect anything in ggplot2

I'm somewhat new to R and ggplot2 so this question is likely somewhat low-level. But I've done a fair amount of experimenting and found no answers online, so I thought I'd ask here.

When I add alpha to my graph, the graph appears as follows:

Some alpha

However, no matter how I change the value of alpha, I get no changes in the graph. I tried alpha=.9 and alpha=1/10000, and there was no difference whatsoever in the graph.

Yet it seems that the 'alpha' term is doing something. When I remove the 'alpha' from the code, I get the following graph:

No alpha

Here's my code. Thanks!

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()
like image 621
user1807967 Avatar asked Nov 08 '12 02:11

user1807967


1 Answers

You need to put the alpha specification outside the mapping statement, as in

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(),
)       

I'm more used to expressing this somewhat more compactly as

geom_point(data=mydata,
           mapping=aes(x=day, y=seconds),
           alpha=1/2,size=5)

The rest of the excluded stuff represents default values, I believe ...

See also: Why does the ggplot legend show the "colour" parameter?

like image 90
Ben Bolker Avatar answered Sep 20 '22 13:09

Ben Bolker