I am trying to get a boxplot with 3 different tools in each dataset size like the one below:
ggplot(data1, aes(x = dataset, y = time, color = tool)) + geom_boxplot() +
labs(x = 'Datasets', y = 'Seconds', title = 'Time') +
scale_y_log10() + theme_bw()
But I need to transform x-axis to log scale. For that, I need to numericize each dataset to be able to transform them to log scale. Even without transforming them, they look like the one below:
ggplot(data2, aes(x = dataset, y = time, color = tool)) + geom_boxplot() +
labs(x = 'Datasets', y = 'Seconds', title = 'Time') +
scale_y_log10() + theme_bw()
I checked boxplot parameters and grouping parameters of aes, but could not resolve my problem. At first, I thought this problem is caused by scaling to log, but removing those elements did not resolve the problem.
What am I missing exactly? Thanks...
Files are in this link. "data2" is the numericized version of "data1".
Your question was a tough cookie, but I learned something new from it!
Just using group = dataset is not sufficient because you also have the tool variable to look out for. After digging around a bit, I found this post which made use of the interaction() function.
This is the trick that was missing. You want to use group because you are not using a factor for the x values, but you need to include tool in the separation of your data (hence using interaction() which will compute the possible crosses between the 2 variables).
# This is for pretty-printing the axis labels
my_labs <- function(x){
paste0(x/1000, "k")
}
levs <- unique(data2$dataset)
ggplot(data2, aes(x = dataset, y = time, color = tool,
group = interaction(dataset, tool))) +
geom_boxplot() + labs(x = 'Datasets', y = 'Seconds', title = 'Time') +
scale_x_log10(breaks = levs, labels = my_labs) + # define a log scale with your axis ticks
scale_y_log10() + theme_bw()
This plots
When I combine geom_vline() with facet_grid() like so:
DATA <- data.frame(x = 1:6,y = 1:6, f = rep(letters[1:2],3))
ggplot(DATA,aes(x = x,y = y)) +
geom_point() +
facet_grid(f~.) +
geom_vline(xintercept = 2:3,
colour =c("goldenrod3","dodgerblue3"))
I get an error message stating Error: Aesthetics must be either length 1 or the same as the data (4): colour because there are two lines in each facet and there are two facets. One way to get around this is to use rep(c("goldenrod3","dodgerblue3"),2), but this requires that every time I change the faceting variables, I also have to calculate the number of facets and replace the magic number (2) in the call to rep(), which makes re-using ggplot code so much less nimble.
Is there a way to get the number of facets directly from ggplot for use in this situation?
You could put the xintercept and colour info into a data.frame to pass to geom_vline and then use scale_color_identity.
ggplot(DATA, aes(x = x, y = y)) +
geom_point() +
facet_grid(f~.) +
geom_vline(data = data.frame(xintercept = 2:3,
colour = c("goldenrod3","dodgerblue3") ),
aes(xintercept = xintercept, color = colour) ) +
scale_color_identity()
This side-steps the issue of figuring out the number of facets, although that could be done by pulling out the number of unique values in the faceting variable with something like length(unique(DATA$f)).
I'm trying to make a facet plot of three line plots, and in each line plot there are three lines. I'd like to make the line corresponding to "oirf" to be a solid line, and both lines corresponding to "upperCI" and "lowerCI" to be dashed, either the same kind of dashed line or something very similar looking, like so:
But I'm getting an error I can't resolve.
Here's my data:
"countrySpellId","iso","country","step","indicator","vals"
38,"BLR","Belarus",0,"oirf",-0.19979745478058
38,"BLR","Belarus",1,"oirf",-0.182586795026907
38,"BLR","Belarus",2,"oirf",-0.242312010909111
106,"GEO","Georgia",0,"oirf",-0.154580915298088
106,"GEO","Georgia",1,"oirf",-0.0572862343086547
106,"GEO","Georgia",2,"oirf",-0.345457860190889
167,"KGZ","Kyrgyzstan",0,"oirf",0.960777168532119
167,"KGZ","Kyrgyzstan",1,"oirf",0.458003383067036
167,"KGZ","Kyrgyzstan",2,"oirf",0.190725669245905
38,"BLR","Belarus",0,"lowerCI",-0.357909781851253
38,"BLR","Belarus",1,"lowerCI",-0.411483619567094
38,"BLR","Belarus",2,"lowerCI",-0.514508124910321
106,"GEO","Georgia",0,"lowerCI",-0.323219475085121
106,"GEO","Georgia",1,"lowerCI",-0.236286319570866
106,"GEO","Georgia",2,"lowerCI",-0.540228716700013
167,"KGZ","Kyrgyzstan",0,"lowerCI",0.448913075973564
167,"KGZ","Kyrgyzstan",1,"lowerCI",0.0581860926615476
167,"KGZ","Kyrgyzstan",2,"lowerCI",-0.235580302805703
38,"BLR","Belarus",0,"upperCI",-0.0416851277099078
38,"BLR","Belarus",1,"upperCI",0.0463100295132811
38,"BLR","Belarus",2,"upperCI",0.0298841030920997
106,"GEO","Georgia",0,"upperCI",0.0140576444889454
106,"GEO","Georgia",1,"upperCI",0.121713850953557
106,"GEO","Georgia",2,"upperCI",-0.150687003681766
167,"KGZ","Kyrgyzstan",0,"upperCI",1.47264126109067
167,"KGZ","Kyrgyzstan",1,"upperCI",0.857820673472524
167,"KGZ","Kyrgyzstan",2,"upperCI",0.617031641297513
Here's how I'm trying to do the plots:
ggplot(data = oirfsFacetPlot2, aes(x = step, y = vals, group = countrySpellId,
stat = "identity")) +
geom_line(aes(linetype = indicator)) +
xlab("Month") + ylab("Percent change") +
theme_bw() + scale_x_continuous(breaks = seq(0,3,1)) +
scale_linetype_discrete(name ="indicator",
breaks=c("lowerCI", "oirf","upperCI")) +
facet_wrap( ~ country, scales = "free_y", nrow = 3 )
But then I get this error, which is somehow related to the aes(linetype = indicator) I think.
Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line
What am I doing wrong?
As Richard pointed out, deleting the group=countrySpellId will eliminate the error, because you are trying to apply a group twice on two different variables (the linetype argument essentially does the same thing as group, it just means that that the different lines will also have different linetypes). The grouping on country will happen later in the facet_wrap.
Just doing that will get you three different linetypes that ggplot automatically chooses for you, but since you're particular about how those lines should look, you'll want to use scale_linetype_manual, which allows you to specify what linetype ggplot assigns to each factor level. You were on the right tract with scale_linetype_discrete!
Deleting the bad group argument and replacing the scale_lientype_discrete we have:
ggplot(data = oirfsFacetPlot2, aes(x = step, y = vals, stat = "identity")) +
geom_line(aes(linetype = indicator)) +
xlab("Month") + ylab("Percent change") +
theme_bw() + scale_x_continuous(breaks = seq(0,3,1)) +
scale_linetype_manual(name = "indicator", values = c(2,1,2)) +
facet_wrap( ~ country, scales = "free_y", nrow = 3 )
I'm struggling with facet_wrap in R. It should be simple however the facet variable is not being picked up? Here is what I'm running:
plot = ggplot(data = item.household.descr.count, mapping = aes(x=item.household.descr.count$freq, y = item.household.descr.count$descr, color = item.household.descr.count$age.cat)) + geom_point()
plot = plot + facet_wrap(~ age.cat, ncol = 2)
plot
I colored the faceting variable to try to help illustrate what is going on. The plot should have only one color in each facet instead of what you see here. Does anyone know what is going on?
This error is caused by fact that you are using $and data frame name to refer to your variables inside the aes(). Using ggplot() you should only use variables names in aes() as data frame is named already in data=.
plot = ggplot(data = item.household.descr.count,
mapping = aes(x=freq, y = descr, color = age.cat)) + geom_point()
plot = plot + facet_wrap(~ age.cat, ncol = 2)
plot
Here is an example using diamonds dataset.
diamonds2<-diamonds[sample(nrow(diamonds),1000),]
ggplot(diamonds2,aes(diamonds2$carat,diamonds2$price,color=diamonds2$color))+geom_point()+
facet_wrap(~color)
ggplot(diamonds2,aes(carat,price,color=color))+geom_point()+
facet_wrap(~color)
I'm trying to add panel labels to different facets in a plot. I want them to be 1:7, but, the following code
d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
xlim(0, 2) + stat_binhex(na.rm = TRUE) + opts(aspect.ratio = 1)
d1<-d + facet_wrap(~ color)
d1+annotate("text", x=0.25, y=1.5e+04, label=1:7)
yields
Error: When _setting_ aesthetics, they may only take one value. Problems: label
Now, I can supply a single value and get that replicated across all facets. But how can I have different labels in different facets using annotate()?
With annotate, you can't. But by setting up a data.frame and using it as the data source for a geom_text, it is easy (with a few bookkeeping aspects).
d1 + geom_text(data=data.frame(x=0.25, y=1.5e+04, label=1:7,
color=c("D","E","F","G","H","I","J")),
aes(x,y,label=label), inherit.aes=FALSE)