How can I prevent ggplot2 from overflowing window? - r

I'm running R 3.1.1 with ggplot2_1.0.0. I'm having trouble with the default layout. I would expect ggplot to do a better job avoiding the overflow you see here. It feels like ggplot thinks that my device is much larger than it actually is. I'm running this on Ubuntu 14.04, FWIW.
For something more reproducible, I run this:
ggplot(mtcars, aes(x=cyl,y=hp,color=as.factor(mpg))) + geom_point()
And get this, where the legends have really huge boxes.

Here is one solution. I used the cut argument to specify ranges over mpg. Using cut will automatically coerce the variable to a factor as well.
library(ggplot2)
ggplot(mtcars, aes(x=cyl,y=hp,color=cut(mpg,quantile(mpg,seq(0,1,by=.25))))) +
geom_point() + scale_color_discrete(name="Legend Title (mpg breaks)")

Related

ggplot2 stacked bar graph custom order rather than size only works for specific level in facet_grid

I am currently trying to reorder the stacks in a stacked bargraph with ggplot2 using the order argument. This works like a charm when not using facetting, but upon running facet_grid, only one of the pannels works. It is important to realize that I want to use a custom order, rather than based on size (as suggested in this topic or others concerning the use of order=desc(variable)).
You should be able to reproduce this issue using the code below
require(dplyr)
require(tidyr)
require(ggplot2)
prod<-1:8
timing<-1:4
variab<-letters[1:3]
nobserv<-length(prod)*length(timing)*length(variab)
values<-runif(nobserv)
plotset<-data.frame(product=rep(prod,each=4,length=nobserv),
timepoints=rep(timing,length=nobserv),
variable=rep(variab,each=nobserv/3),
values=values)
sorter<-rep(c(-2,-1,-3),each=nobserv/3)
p <- ggplot(plotset,aes(fill=variable,order=sorter,x=product,y=as.numeric(as.character(values))))
p <- p + geom_bar(stat='identity', position='stack')
p <- p + facet_grid(~ timepoints)
p
This will generate this graph, clearly showing only timepoint three (3) does what I want.
I do not know why this is and if and how it could be fixed.
Thanks in advance for your input!
EDIT: I made a typo, now the issue should be reproducible
ps: I could reproduce the problem on both a linux and a windows machine with R 3.1.1 and ggplot2 1.0.0

R ggplot2 scales cancel each other

I'm having some trouble plotting with ggplot2. When trying to use 2 different scale functions, they won't act at the same time, that is, only one command will actually work, depending on the order. For example, if I do plot + scale_x_discrete(...) and then plot + scale_fill_discrete(...), only the later will work (editing the legend), while the other wont, leaving the x axis unedited. If I switch the order of commands, then the axis is edited, while the legend is neglected.
Could you please explain why this is happening and how I should be doing this?
Did you try
plot + scale_x_discrete(...) + scale_fill_discrete(...)

How to make an R barplot with a log y-axis scale?

This should be a simple question... I'm just trying to make a barplot from a vector in R, but want the values to be shown on a log scale, with y-axis tick marks and labelling. I can make the normal barplot just fine, but when I try to use log or labelling, things go south.
Here is my current code:
samples <- c(10,2,5,1,2,2,10,20,150,23,250,2,1,500)
barplot(samples)
Ok, this works. Then I try to use the log="" function defined in the barplot manual, and it never works. Here are some stupid attempts I have tried:
barplot(samples, log="yes")
barplot(samples, log="TRUE")
barplot(log=samples)
Can someone please help me out here? Also, the labelling would be great too. Thanks!
The log argument wants a one- or two-character string specifying which axes should be logarithmic. No, it doesn't make any sense for the x-axis of a barplot to be logarithmic, but this is a generic mechanism used by all of "base" graphics - see ?plot.default for details.
So what you want is
barplot(samples, log="y")
I can't help you with tick marks and labeling, I'm afraid, I threw over base graphics for ggplot years ago and never looked back.
This should get your started fiddling around with ggplot2.
d<-data.frame(samples)
ggplot(data=d, aes(x=factor(1:length(samples)),y=samples)) +
geom_bar(stat="identity") +
scale_y_log10()
Within the scale_y_log10() function you can define breaks, labels, and more. Similarly, you can label the x-axis. For example
ggplot(data=d, aes(x=factor(1:length(samples)),y=samples)) +
geom_bar(stat="identity") +
scale_y_log10(breaks=c(1,5,10,50,100,500,1000),
labels=c(rep("label",7))) +
scale_x_discrete(labels=samples)

How to make ggplot2 graphics compatible with black-and-white printing (photocopy friendly) in R?

Is there a way to convert ggplot2 plots into black-and-white versions without rewriting much of their code, so that the black-and-white versions remain readable?
For instance, to replace scale_fill_gradient with scale_fill_grey? Or automatically make photocopy-friendly transformations as sites like http://colorbrewer2.org/ advise. (Unfortunately, textures are not an option, as ggplot2 doesn't support them.)
It's clearly possible with if ... else and custom functions, but is there a more general solution?
My second line in every ggplot figure I make is
theme_bw()
so
qplot(mpg, wt, data = mtcars) +
theme_bw()

Indicating the statistically significant difference in bar graph USING R

This is a repeat of a question originally asked here: Indicating the statistically significant difference in bar graph but asked for R instead of python.
My question is very simple. I want to produce barplots in R, using ggplot2 if possible, with an indication of significant difference between the different bars, e.g. produce something like this. I have had a search around but can't find another question asking exactly the same thing.
I know that this is an old question and the answer by Didzis Elferts already provides one solution for the problem. But I recently created a ggplot-extension that simplifies the whole process of adding significance bars: ggsignif
Instead of tediously adding the geom_path and annotate to your plot you just add a single layer geom_signif:
library(ggplot2)
library(ggsignif)
ggplot(iris, aes(x=Species, y=Sepal.Length)) +
geom_boxplot() +
geom_signif(comparisons = list(c("versicolor", "virginica")),
map_signif_level=TRUE)
Full documentation of the package is available at CRAN.
You can use geom_path() and annotate() to get similar result. For this example you have to determine suitable position yourself. In geom_path() four numbers are provided to get those small ticks for connecting lines.
df<-data.frame(group=c("A","B","C","D"),numb=c(12,24,36,48))
g<-ggplot(df,aes(group,numb))+geom_bar(stat="identity")
g+geom_path(x=c(1,1,2,2),y=c(25,26,26,25))+
geom_path(x=c(2,2,3,3),y=c(37,38,38,37))+
geom_path(x=c(3,3,4,4),y=c(49,50,50,49))+
annotate("text",x=1.5,y=27,label="p=0.012")+
annotate("text",x=2.5,y=39,label="p<0.0001")+
annotate("text",x=3.5,y=51,label="p<0.0001")
I used the suggested method from above, but I found the annotate function easier for making lines than the geom_path function. Just use "segment" instead of "text". You have to break things up by segment and define starting and ending x and y values for each line segment.
example for making 3 lines segments:
annotate("segment", x=c(1,1,2),xend=c(1,2,2), y= c(125,130,130), yend=c(130,130,125))

Resources