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()
Related
I'm making a box-and-whisker plot in R (y-axis # of reads and x-axis of 4 discrete conditions). I'm trying to switch the order in which the discrete conditions appear and to change them from the default white fill to a color of my choosing using the code below. I can get the order to change, but the color continues to stay white. I also have no idea why R cuts off my plot.
library(ggplot2)
capture_data = read.csv("tcp_for_r_plots.csv")
p <- ggplot(capture_data, aes(x=Protocol, y=raw_reads)) + geom_boxplot()
p <- p + scale_x_discrete(limits=c("Standard","TD-60","TD-55","TD-50"))
p <- p + scale_fill_manual(values=c("#999999","#FFFF00","#33FFFF","#FF33CC"))
Attached is the output I keep getting - no color change.
fill color: You need to add the fill option to the geom_boxplot() function as shown below (instead of using the scale_fill_manual function):
+ geom_boxplot(fill=c("#999999","#FFFF00","#33FFFF","#FF33CC"))
Order: the order is based on the alphabetical order of the factor values (Protocol). One solution is to recode the factor levels into the the order you want before running the generating the plot.
p <- ggplot(capture_data, aes(x=Protocol, y=raw_reads, fill=Protocol)) +
geom_boxplot() +
scale_x_discrete(limits=c("Standard","TD-60","TD-55","TD-50")) +
scale_fill_manual(values=c("#999999","#FFFF00","#33FFFF","#FF33CC"))
Add the colors in "fill" argument in ggplot:
p <- ggplot(capture_data, aes(x=Protocol, y=raw_reads)) + geom_boxplot()
should be
p <- ggplot(capture_data, aes(x=Protocol, y=raw_reads, fill = Protocol)) + geom_boxplot()
For example,
ggplot(mtcars, aes(x= as.factor(cyl), y=mpg, fill=as.factor(cyl))) + geom_boxplot()
gives me
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 want to manually set the range of Y axis. I want manually increase values of Y axis by 30 units. I have a following code:
library(scales)
library(ggplot2)
ggplot(mtcars,aes(x=wt,y=mpg)) +
geom_point() +
geom_smooth() +
scale_y_continuous(trans = ~.+30)
But I got the following error message:
Error in match.fun(f) :
'c("~_trans", ". + 30_trans")' is not a function, character or symbol
Can somebody help me?
This is the plot what I want:
If you just want to change the labels, then this will do it.
ggplot(mtcars, aes(x=wt,y=mpg)) +
geom_point() +
geom_smooth() +
scale_y_continuous(labels=function(x) x+30)
I am trying to color bars in ggplot but having issues. Can someone explain how to correctly use the fill parameter and the scale_colour parameters?
library(ggplot2)
df<-data.frame(c(80,33,30),c("Too militarized","Just doing their job","Unfairly tarnished by a few"),c("57%","23%","21%"))
colnames(df)<-c("values","names","percentages")
ggplot(df,aes(names,values))+
geom_bar(stat = "identity",position = "dodge",fill=names)+
geom_text(aes(label=percentages), vjust=0)+
ylab("percentage")+
xlab("thought")+
scale_colour_manual(values = rainbow(nrow(df)))
Working barplot example
barplot(c(df$values),names=c("Too militarized","Just doing their job","Unfairly tarnished by a few"),col = rainbow(nrow(df)))
The main issue is that you don't have fill inside a call to aes in geom_bar(). When mapping from data to visuals like colors, it has to be inside aes(). You can fix this by either wrapping fill=names with aes() or by just specifying fill colors directly, instead of using names:
Option 1 (no legend):
ggplot(df, aes(names, values)) +
geom_bar(stat="identity", fill=rainbow(nrow(df))) +
ylab("percentage") +
xlab("thought")
Option 2 (legend, because mapping from data to colors):
ggplot(df, aes(names, values)) +
geom_bar(stat="identity", aes(fill=names)) +
ylab("percentage") +
xlab("thought") +
scale_fill_manual(values=rainbow(nrow(df)))
Note that in both cases you might want to explicitly factor df$names ahead of the call to ggplot in order to get the bars in the order you want.
I get errors when I use geom_abline in the same plot as facet_wrap or facet_grid, and I don't understand why. For example
# Example data
ex <- data.frame(x=1:10, y=1:10, f=gl(2, 5))
ggplot() +
geom_point(data=ex, aes(x=x, y=y)) +
geom_abline(slope=1, intercept=0) +
facet_wrap(~f)
causes Error in if (empty(data)) { : missing value where TRUE/FALSE needed.
Above I set the data in the geom_point layer because later on I will add data from a different data frame. This has something to do with the problem, because when I set the data in the base layer I get a different error:
ggplot(ex, aes(x=x, y=y)) +
geom_abline(slope=1, intercept=0) +
facet_wrap(~f)
Error in as.environment(where) : 'where' is missing
Workaround
There's an easy workaround: If I make a data frame to define a 1:1 line and plot it using geom_line I get essentially the same plot I would have gotten from geom_abline...
# Define a 1:1 line with data
one_to_one <- data.frame(xO=range(ex$totalcells), yO=range(ex$totalcells))
# Plot the 1:1 line with geom_line
ggplot() +
geom_point(data=ex, aes(x=x, y=y)) +
geom_line(data=one_to_one, aes(x=xO, y=yO), colour="black") +
facet_wrap(~f)
...so this question is more about why those errors arise (and whether they represent a bug or expected behavior) rather than how to work around the problem.
The following works:
ggplot(ex, aes(x=x, y=y)) + geom_point() +
geom_abline(slope=1, intercept=0) +
facet_wrap(~f)
Note the additional geom_point() I added, based on your second example.