7

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()
user1807967
  • 137
  • 1
  • 1
  • 6
  • 1
    Size and alpha are arguments to layer() not aes_string. You could just use geom_point() directly. Especially since you're only graphing one geom. For example: ... + geom_point(mydata,aes(day,seconds),stat="identity",position="identity",alpha=0.5,size=5). That seems cleaner to me but I can't test this without you data – Brandon Bertelsen Nov 08 '12 at 03:04

1 Answers1

4

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?

Community
  • 1
  • 1
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Hmm, putting it in `geom_params=list()` worked, thought putting it where you put it didn't seem to work. Not totally sure what the deal is - I think I'll be sticking with the compact phrasing from now on. Thanks. – user1807967 Nov 08 '12 at 03:17