This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using ggplot2, can I insert a break in the axis?
I'm using the following ggplot2 code to generate a faced_grid barplots:
ggplot(plotobj, aes(as.factor(gm) , peaks, fill=rvalue)) +
geom_bar(stat="identity") +
facet_grid(rvalue ~ .,scales="free") +
opts(legend.position = "none")
Which gives the following plot (screenshot of the first facet):
As you can see the y-axis get stretched to quite a high value because of 1 outlier. What I'd like to do is create a more sensible scaling by having more ticks until 2e+05 and then just have 1 tick that goes directly towards 5e+05. This way the scaling would not be linear anymore but it would allow to show that there is a massive peak for 1 of the categories.
Is there anyway of doing this simple with ggplot2? Is there a R trick for doing this? If possible I'd not like to use things like ylim to just not show the top anymore.
You could use a transformation on the y-axis. Untested since you did not provide a reproducible example.
ggplot(plotobj, aes(as.factor(gm) , peaks, fill=rvalue)) +
geom_bar(stat="identity") + facet_grid(rvalue ~ .,scales="free") +
opts(legend.position = "none") + scale_y_log10()
ggplot(plotobj, aes(as.factor(gm) , peaks, fill=rvalue)) +
geom_bar(stat="identity") + facet_grid(rvalue ~ .,scales="free") +
opts(legend.position = "none") + scale_y_sqrt()
ggplot(plotobj, aes(as.factor(gm) , peaks, fill=rvalue)) +
geom_bar(stat="identity") + facet_grid(rvalue ~ .,scales="free") +
opts(legend.position = "none") + coord_trans(y = "log10")
ggplot(plotobj, aes(as.factor(gm) , peaks, fill=rvalue)) +
geom_bar(stat="identity") + facet_grid(rvalue ~ .,scales="free") +
opts(legend.position = "none") + coord_trans(y = "sqrt")
Related
I am new to R, and I'm trying to use ggplot2 to plot some data as a scatterplot. I'm missing a day in my samples, and the trendline I made won't connect all of the data together. Below is the code I have and what the graph looks like.
ggplot(SiExptTEPa, aes(x=Timepoint..dpi., y=(TEPcells),group=(Treatment))) +
geom_point(size=5,aes(colour=Nutrient)) +
scale_color_manual(values=c('yellow','light blue')) +
geom_errorbar(aes(ymin=TEPcells-se, ymax=TEPcells+se), width=.1) +
facet_wrap(~Nutrient, scales="free") +
scale_y_continuous(labels = scientific) +
theme_classic() +
xlab("Time Post Infection (Days)") +
ylab("TEP by Total Cells") +
ylim(3e-08,2e-07) +
geom_line(aes(linetype=Treatment)) +
scale_linetype_manual(values=c("solid", "dashed"))
Incorrect graph
Please tell me how to connect the gap in the middle of both sides of the graph so that there is one continuous line?
I draw a barchart in R:
ggplot(data, aes(x=rating, fill=rating)) +
geom_bar(stat="count") +
ggtitle("Rating in stories")+
coord_flip()+
xlab("rating")+
ylab("number of stories")+
theme(legend.position="none")
The result is here.
The bars represent the amount of times the specific value (M, T, K or K+) occurs in the rating variable.
How do I sort the bars decreasingly?
OK, I found what I was looking for. I needed to use fct_rev(fct_infreq()) on the variable.
ggplot(data, aes(forcats::fct_rev(fct_infreq(rating)), fill=rating)) +
geom_bar(stat = "count") +
ggtitle("Rating in stories")+
coord_flip()+
xlab("rating")+
ylab("number of stories")+
theme(legend.position="none")
My dataset:
reading,depth
31055,0.0
57635,0.5
34268,1.0
20926,1.5
13507,2.0
12944,2.5
13002,3.0
12892,3.5
12610,4.0
12158,4.5
12004,5.0
I'm plotting an instrument reading as a function of below-ground depth. Because of what I'm visualizing, I want the axes to have a non-standard configuration, like this:
ggplot(data=f0101, aes(x=reading, y=depth)) +
theme_classic() +
scale_y_reverse() +
scale_x_continuous(position="top") +
geom_path()
This is perfect, except that I'd like the line smoothed, via stat_smooth() or similar. But, calling a smooth appears to throw off geom_path().
ggplot(data=f0101, aes(x=reading, y=depth)) +
theme_classic() +
scale_y_reverse() +
scale_x_continuous(position="top") +
geom_path() +
stat_smooth()
What can I do to get the smoothing effect of stat_smooth() while still plotting the data in an admittedly non-standard configuration?
Thank you!
I may be wrong, but I think you probably want something like this:
dat %>%
ggplot(aes(x = depth, y = reading)) +
geom_path() +
stat_smooth() +
theme_classic() +
scale_x_reverse() +
scale_y_continuous(position = "top") +
coord_flip()
which produces
For this, you're running the smooth with depth on the x and reading on the y, then flipping the coordinates.
This question already has answers here:
geom_bar bars not displaying when specifying ylim
(4 answers)
Limit ggplot2 axes without removing data (outside limits): zoom
(1 answer)
Closed 7 years ago.
My data disappears when I truncate the y-axis to show only the 50 to 90 range. Am I putting the ylim in the wrong place?
The spreadsheet looks like this:
xval yval_LWTW linenames SNP
1 61.4835166 MT9513 NN
2 61.93341478 RITA GG
3 63.31277751 JUDITH CC
4 63.60466558 CO04W320 GG
5 64.84700514 DECADE NN
library(ggplot2)
library(xlsx)
data <- read.xlsx("RdataForGraphsofBestHits.xlsx", sheetIndex=4)
ggplot(data, aes(x=xval, y=yval_LWTW, fill=SNP,
scale_fill_manual(values=c(GG="blue",CC="red",NN="green")))) +
geom_bar(stat="identity", width=1) +
theme(axis.title.x=element_blank())
This gives me a tall plot because the data go from 61 to 81. I want to make the y-axis start at 50, so the plot will be shorter, so I add + ylim(50.0, 90.0) to the code.
library(ggplot2)
library(xlsx)
data <- read.xlsx("RdataForGraphsofBestHits.xlsx", sheetIndex=4)
ggplot(data, aes(x=xval, y=yval_LWTW, fill=SNP,
scale_fill_manual(values=c(GG="blue", CC="red", NN="green")))) +
geom_bar(stat="identity", width=1) +
theme(axis.title.x=element_blank()) +
ylim(50.0, 90.0)
Now I get the gray background of the plot with the y-axis nicely limited to the 50 to 90 range, but no data plotted onto it. Just the gray background and the axis labels and the legend.
I think I have the code for making the plot horizontal.
ggplot(data, aes(x=xval, y=yval_LWTW, fill=SNP,
scale_fill_manual(values=c(GG="blue", CC="red", NN="green")))) +
geom_bar(stat="identity", width=1) +
theme(axis.title.x=element_blank()) +
coord_fixed(ratio=1/2)
So I can fix the horizontal problem if I can fix the disappearing data problem.
Was curious so I coded it up:
n <- 250
xval <- 1:n
yval <- 61 + 4*sin(2*pi*(1:n)/n) + 18*(1:n)/n
snp <- sample(c("CC",rep("GG",40),rep("NN",40)),n,replace=T)
data <- data.frame(xval=xval,yval_LWTW=yval,SNP=snp)
ggplot(data, aes(x=xval, y=yval_LWTW, fill=SNP)) +
scale_fill_manual(values=c(GG="blue",CC="red",NN="green")) +
geom_bar(stat="identity", width=1) +
theme(axis.title.x=element_blank())
Yields:
And this:
ggplot(data, aes(x=xval, y=yval_LWTW, fill=SNP)) +
scale_fill_manual(values=c(GG="blue",CC="red",NN="green")) +
geom_bar(stat="identity", width=1) +
theme(axis.title.x=element_blank()) +
ylim(50,90)
Yields:
And user20650's coord_cartesian suggestion:
ggplot(data, aes(x=xval, y=yval_LWTW, fill=SNP)) +
scale_fill_manual(values=c(GG="blue",CC="red",NN="green")) +
geom_bar(stat="identity", width=1) +
theme(axis.title.x=element_blank()) +
coord_cartesian(ylim=c(50,90))
yields this:
I would like to have a boxplot showing the same distribution underneath my histogram.
The code below almost works, but coord_flip() is being applied to all layers, instead of just the geom_boxplot layer.
plot1<-ggplot(newdatahistogram, aes_string(x=newdatahistogram[RawLocation])) +
xlab(GGVar) + ylab("Proportion of Instances") +
geom_histogram(aes(y=..density..), binwidth=1, colour="black", fill="white",origin=-0.5) +
scale_x_continuous(limits=c(-3,6), breaks=seq(0,5,by=1), expand=c(.01,0)) +
geom_boxplot(aes_string(x=-1, y=newdatahistogram[RawLocation])) + coord_flip()
How can I apply coord_flip() to a single layer?
Thank you!
I got it to work with a bit of a hack;
plot1 <- ggplot(newdatahistogram, aes_string(x=newdatahistogram[RawLocation], fill=(newdatahistogram[,"PQ"]))) +
xlab(GGVar) + ylab("Proportion of Observation") +
geom_histogram(aes(y=..density..), binwidth=1, colour="black", origin=-0.5) +
scale_x_continuous(limits=c(-1,6), breaks=seq(0,5,by=1), expand=c(.01,0)) +
scale_y_continuous(limits=c(-.2,1), breaks=seq(0,1,by=.2))
theme(plot.margin = unit(c(0,0,0,0), "cm"))
plot_box <- ggplot(newdatahistogram) +
geom_boxplot(aes_string(x=1, y=newdatahistogram[RawLocation])) +
scale_y_continuous(breaks=(0:5), labels=NULL, limits=c(-1,6), expand=c(.0,-.03)) +
scale_x_continuous(breaks=NULL) + xlab(NULL) + ylab(NULL) +
coord_flip() + theme_bw() +
theme(plot.margin = unit(c(0,0,.0,0), "cm"),
line=element_blank(),text=element_blank(),
axis.line = element_blank(),title=element_blank(), panel.border=theme_blank())
PB = ggplotGrob(plot_box)
plot1 <- plot1 + annotation_custom(grob=PB, xmin=-1.01, xmax=5.95, ymin=-.3,ymax=0)
This saves the rotated boxplot as a grob object and inserts it into the plot under the histogram.
I needed to play with the expansion element a bit to get the scales to line up,
but it works!
Seriously though, I think ggplot should have a horizontal boxplot available without cord_flip()... I tried to edit the boxplot code, but it was way too difficult for me!
Tried to post image, but not enough reputation
You can't: coord_flip always acts on all layers. However, you do have two alternatives:
The solution here shows how to use grid.arrange() to add a marginal histogram. (The comments in the question also link to a nice base-R way to do the same thing)
You could indicate density using a rug plot on of the four sides of the plot with plot1 + geom_rug(sides='r')
ggplot(mpg, aes(x=class, y=cty)) +
geom_boxplot() + geom_rug(sides="r")