I'm having some trouble adding a vertical line to a plot when the x-axis is a datetime (POSIXct) object. It seems to always want to put the line at the Epoch. Here's an example:
df <- data.frame(x=ymd('2011-01-01')+hours(0:24), y=runif(25))
ggplot(df, aes(x=x,y=y)) + geom_point()
Now I try to add a line at the third observation time:
ggplot(df, aes(x=x,y=y)) + geom_point() + geom_vline(aes(x=df$x[3]))
Something I'm doing wrong?
Try doing this instead:
geom_vline(xintercept = df$x[3])
ggplot(df, aes(x=x,y=y)) + geom_point() + geom_vline(aes(xintercept=df$x[3]))
you want xintercept rather than x in your geom_vline aes.
Related
ggplot(data=df, aes(x='Matcing_Probability', y=Locus_Name, group=1)) +
+ geom_line(color="#aa0022", size=1.75) +
+ geom_point(color="#aa0022", size=3.5)
This is the graph I am getting from the code.
You need to send ggplot2 symbols (unquoted column names) in aes() if you are assigning an aesthetic to a column in your dataset. Otherwise, it will assume you are sending the string of a new symbol. So:
# your original
ggplot(data=df, aes(x='Matching_Probability', y=Locus_Name, group=1))
# change to this:
ggplot(data=df, aes(x=Matching_Probability, y=Locus_Name, group=1))
Consider the difference in the following example to highlight why even more:
# this works fine
df <- data.frame(x=1:10, y=1:10)
ggplot(df, aes(x=x,y=y)) + geom_point()
# this doesn't:
ggplot(df, aes(x="x",y=y)) + geom_point()
I have the following data frame:
df <- data.frame(x=c(1,2,3,4,5),
y=c(2,3,5,9,9),
label=c('blah1','blah2','blah3','blah4','blah5'),
vjust=c('top','bottom','top','bottom','top'),
posVjust=c(0.9,1.1,0.9,1.1,0.9),
stringsAsFactors=FALSE)
and can plot it directly like so:
p <- ggplot(df, aes(x=x,y=y,label=label)) + geom_point() + geom_line() +
geom_text(aes(vjust=vjust))
p
However, I'd like to use the posVjust column as part of geom_text's aes but I can't like so:
geom_text(aes(vjust=vjust,position=position_stack(vjust=posVjust)))
I get the following error:
Warning: Ignoring unknown aesthetics: position
> p
Don't know how to automatically pick scale for object of type
PositionStack/Position/ggproto. Defaulting to continuous.
Error: Aesthetics must be either length 1 or the same as the data (5): vjust,
position, x, y, label
Is there a way to use my posVjust column as part of the position_stack call?
position isn't an aesthetic and goes outside of aes. As far as I know, position_stack takes a single value, rather than a vector. However, you could change posVjust to be posVjust=c(-0.1,0.1,-0.1,0.1,-0.1) and then do the following:
ggplot(df, aes(x=x, y=y,label=label)) + geom_point() + geom_line() +
geom_text(aes(y=y + posVjust))
You could also dispense with posVjust and just do:
ggplot(df, aes(x=x, y=y,label=label)) + geom_point() + geom_line() +
geom_text(aes(y=y + c(-0.1,0.1)))
You can add vjust=vjust as well, which will add a small additional increment of vertical offset.
Another option is to remove the points and just use labels instead of point markers. Offsetting the geom_text labels then becomes unnecessary. For example:
ggplot(df, aes(x=x, y=y, label=label)) +
geom_line(linetype="12", colour="grey50") +
geom_text() +
theme_bw()
I have this simple data frame holding three replicates (value) for each factor (CT). I would like to plot it as geom_point and than the means of the point as geom_line.
gene <- c("Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5")
value <- c(0.86443, 0.79032, 0.86517, 0.79782, 0.79439, 0.89221, 0.93071, 0.87170, 0.86488, 0.91133, 0.87202, 0.84028, 0.83242, 0.74016, 0.86656)
CT <- c("ET","ET","ET", "HP","HP","HP","HT","HT","HT", "LT","LT","LT","P","P","P")
df<- cbind(gene,value,CT)
df<- data.frame(df)
So, I can make the scatter plot.
ggplot(df, aes(x=CT, y=value)) + geom_point()
How do I get a geom_line representing the means for each factor. I have tried the stat_summary:
ggplot(df, aes(x=CT, y=value)) + geom_point() +
stat_summary(aes(y = value,group = CT), fun.y=mean, colour="red", geom="line")
But it does not work.
"geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?"
But each group has three observations, what is wrong?
Ps. I am also interested in a smooth line.
You should set the group aes to 1:
ggplot(df, aes(x=CT, y=value)) + geom_point() +
stat_summary(aes(y = value,group=1), fun.y=mean, colour="red", geom="line",group=1)
You can use the dplyr package to get the means of each factor.
library(dplyr)
group_means <- df %>%
group_by(CT) %>%
summarise(mean = mean(value))
Then you will need to convert the factors to numeric to let you plot lines on the graph using the geom_segment function. In addition, the scale_x_continuous function will let you set the labels for the x axis.
ggplot(df, aes(x=as.numeric(CT), y=value)) + geom_point() +
geom_segment(aes(x=as.numeric(CT)-0.4, xend=as.numeric(CT)+0.4, y=mean, yend=mean),
data=group_means, colour="red") +
scale_x_continuous("name", labels=as.character(df$CT), breaks=as.numeric(df$CT))
Following on from hrbrmstr's comment you can add the smooth line using the following:
ggplot(df, aes(x=as.numeric(CT), y=value, group=1)) + geom_point() +
geom_segment(aes(x=as.numeric(CT)-0.4, xend=as.numeric(CT)+0.4, y=mean, yend=mean),
data=group_means, colour="red") +
scale_x_continuous("name", labels=as.character(df$CT), breaks=as.numeric(df$CT)) +
geom_smooth()
Taking a simple plot from ggplot2 manual
p <- ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point()
p + geom_hline(yintercept=20)
I get a horizontal line at value 20, as advertised.
Is there a way to limit the range of this line on x axis, to let's say2 - 4 range?
You can use geom_segment() instead of geom_hline() and provide x= and xend= values you need.
p+geom_segment(aes(x=2,xend=4,y=20,yend=20))
I am working on finishing up a graph generated using ggplot2 like so...
ggplot(timeSeries, aes(x=Date, y=Unique.Visitors, colour=Revenue))
+ geom_point() + stat_smooth() + scale_y_continuous(formatter=comma)
I have attached the result and you can see the numeric values in the legend for Revenue do not have a comma. How can I add a comma to those values? I was able to use scale_y_continuous for the axis, can that be used for the legend also?
Just to keep current, in ggplot2_0.9.3 the working syntax is:
require(scales)
ggplot(timeSeries, aes(x=Date, y=Unique.Visitors, colour=Revenue)) +
geom_point() +
stat_smooth() +
scale_y_continuous(labels=comma) +
scale_colour_continuous(labels=comma)
Also see this exchange
Note 2014-07-16: the syntax in this answer has been obsolete for some time. Use metasequoia's answer!
Yep - just a matter of getting the right scale_colour_ layer figured out. Try:
ggplot(timeSeries, aes(x = Date, y = Unique.Visitors, colour = Revenue)) +
geom_point() +
stat_smooth() +
scale_y_continuous(formatter = comma) +
scale_colour_continuous(formatter = comma)
I personally would also move my the colour mapping to the geom_point layer, so that it doesn't give you that odd line behind the dot in the legend:
ggplot(timeSeries, aes(x = Date, y = Unique.Visitors)) +
geom_point(aes(colour = Revenue)) +
stat_smooth() +
scale_y_continuous(formatter = comma) +
scale_colour_continuous(formatter = comma)
...as I stumbled over this older thread, maybe it makes sense to add you need to load library("scales"), otherwise you get the following error message
Error in check_breaks_labels(breaks, labels) : object 'comma' not found