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:
Related
Arrival_Frequency Total_Arrival
0-1 2633586
2-4 223079
4-7 5281
7+ 1718
How to get bar plot for this. If use normal geom_bar() it gives the count not the total.
Do you want this?
library(scales)
library(tidyverse)
ggplot(df, aes(x=Arrival_Frequency, y=Total_Arrival))+
geom_bar(position=position_dodge(), stat="identity") +
scale_y_continuous(labels = label_number()) +
ylab("Total Arrival") + xlab("Arrival Frequency")
Your values are wide apart. So, you can think of transforming the values like
ggplot(df, aes(x=Arrival_Frequency, y=Total_Arrival))+
geom_col() +
scale_y_continuous(trans = "log", labels = label_number()) +
ylab("log (Total Arrival)") + xlab("Arrival Frequency")
I would like to built a boxplot in which the 4 factors (N1:N4) are overlaid in the same column. For example with the following data:
df<-data.frame(N=N,Value=Value)
Q<-c("C1","C1","C2","C3","C3","C1","C1","C2","C2","C3","C3","Q1","Q1","Q1","Q1","Q3","Q3","Q4","Q4","Q1","Q1","Q1","Q1","Q3","Q3","Q4","Q4")
N<-c("N2","N3","N3","N2","N3","N2","N3","N2","N3","N2","N3","N0","N1","N2","N3","N1","N3","N0","N1","N0","N1","N2","N3","N1","N3","N0","N1")
Value<-c(4.7,8.61,8.34,5.89,8.36,1.76,2.4,5.01,2.12,1.88,3.01,2.4,7.28,4.34,5.39,11.61,10.14,3.02,9.45,8.8,7.4,6.93,8.44,7.37,7.81,6.74,8.5)
with the following (usual) code, the output is 4 box-plots displayed in 4 columns for the 4 variables
ggplot(df, aes(x=N, y=Value,color=N)) + theme_bw(base_size = 20)+ geom_boxplot()
many thanks
Updated Answer
Based on your comment, here's a way to add marginal boxplots. We'll use the built-in mtcars data frame.
First, some set-up:
library(cowplot)
# Common theme elements
thm = list(theme_bw(),
guides(colour=FALSE, fill=FALSE),
theme(plot.margin=unit(rep(0,4),"lines")))
Now, create the three plots:
# Main plot
p1 = ggplot(mtcars, aes(wt, mpg, colour=factor(cyl), fill=factor(cyl))) +
geom_smooth(method="lm") + labs(colour="Cyl", fill="Cyl") +
scale_y_continuous(limits=c(10,35)) +
thm[-2] +
theme(legend.position = c(0.85,0.8))
# Top margin plot
p2 = ggplot(mtcars, aes(factor(cyl), wt, colour=factor(cyl))) +
geom_boxplot() + thm + coord_flip() + labs(x="Cyl", y="")
# Right margin plot
p3 = ggplot(mtcars, aes(factor(cyl), mpg, colour=factor(cyl))) +
geom_boxplot() + thm + labs(x="Cyl", y="") +
scale_y_continuous(limits=c(10,35))
Lay out the plots and add the legend:
plot_grid(plotlist=list(p2, ggplot(), p1, p3), ncol=2,
rel_widths=c(5,1), rel_heights=c(1,5), align="hv")
Original Answer
You can overlay all four boxplots in a single column, but the plot will be unreadable. The first example below removes N as the x coordinate, but keeps N as the colour aesthetic. This results in the four levels of N being plotted at a single tick mark (which I've removed by setting breaks to NULL). However, the plots are still dodged. To plot them one on top of the other, set the dodge width to zero, as I've done in the second example. However, the plots are not readable when they are overlaid.
ggplot(df, aes(x="", y=Value,color=N)) +
theme_bw(base_size = 20) +
geom_boxplot() +
scale_x_discrete(breaks=NULL) +
labs(x="")
ggplot(df, aes(x="", y=Value,color=N)) +
theme_bw(base_size = 20) +
geom_boxplot(position=position_dodge(0)) +
scale_x_discrete(breaks=NULL) +
labs(x="")
I am trying to plot the outliers and mean point for the box plots in below using the data available here. The dataset has 3 different factors and 1 value column for 3600 rows.
While I run the below the code it shows the mean point but doesn't draw the outliers properly
ggplot(df, aes(x=Representations, y=Values, fill=Methods)) +
geom_boxplot() +
facet_wrap(~Metrics) +
stat_summary(fun.y=mean, colour="black", geom="point", position=position_dodge(width=0.75)) +
geom_point() +
theme_bw()
Again, while I am modify the code like in below the mean points disappear !!
ggplot(df, aes(x=Representations, y=Values, colour=Methods)) +
geom_boxplot() +
facet_wrap(~Metrics) +
stat_summary(fun.y=mean, colour="black", geom="point", position=position_dodge(width=0.75)) +
geom_point() +
theme_bw()
In both of the cases I am getting the message: "ymax not defined: adjusting position using y instead" 3 times.
Any kind suggestions how to fix it? I would like to draw the mean points within individual box plots and show outliers in the same colour as the plots.
EDIT:
The original data set does not have any outliers and that was reason for my confusion. Thanks to MrFlick's answer with randomly generated data which clarifies it properly.
Rather than downloading the data, I just made a random sample.
set.seed(18)
gg <- expand.grid (
Methods=c("BC","FD","FDFND","NC"),
Metrics=c("DM","DTI","LB"),
Representations=c("CHG","QR","HQR")
)
df <- data.frame(
gg,
Values=rnorm(nrow(gg)*50)
)
Then you should be able to create the plot you want with
library(ggplot2)
ggplot(df, aes(x=Representations, y=Values, fill=Methods)) +
geom_boxplot() +
stat_summary(fun.y="mean", geom="point",
position=position_dodge(width=0.75), color="white") +
facet_wrap(~Metrics)
which gave me
I was using ggplot2 version 0.9.3.1
By using R, is it possible to place 2 ggplot together (i.e., on the same plot)? I wish to show a trend from 2 different data frames and instead of putting them one next to the other, I'd like to integrate them together in one plot and only to change the color of one of them (the black dot).
To be more specific, I have the following 2 visuals:
ggplot(visual1, aes(ISSUE_DATE,COUNTED)) + geom_point() + geom_smooth(fill="blue", colour="darkblue", size=1)
and
ggplot(visual2, aes(ISSUE_DATE,COUNTED)) + geom_point() + geom_smooth(fill="red", colour="red", size=1)
They look like this (both have black dots and I'll need to change one of them to something different):
and
Creating a single combined plot with your current data set up would look something like this
p <- ggplot() +
# blue plot
geom_point(data=visual1, aes(x=ISSUE_DATE, y=COUNTED)) +
geom_smooth(data=visual1, aes(x=ISSUE_DATE, y=COUNTED), fill="blue",
colour="darkblue", size=1) +
# red plot
geom_point(data=visual2, aes(x=ISSUE_DATE, y=COUNTED)) +
geom_smooth(data=visual2, aes(x=ISSUE_DATE, y=COUNTED), fill="red",
colour="red", size=1)
however if you could combine the data sets before plotting then ggplot will
automatically give you a legend, and in general the code looks a bit cleaner
visual1$group <- 1
visual2$group <- 2
visual12 <- rbind(visual1, visual2)
p <- ggplot(visual12, aes(x=ISSUE_DATE, y=COUNTED, group=group, col=group, fill=group)) +
geom_point() +
geom_smooth(size=1)
Dummy data (you should supply this for us)
visual1 = data.frame(ISSUE_DATE=runif(100,2006,2008),COUNTED=runif(100,0,50))
visual2 = data.frame(ISSUE_DATE=runif(100,2006,2008),COUNTED=runif(100,0,50))
combine:
visuals = rbind(visual1,visual2)
visuals$vis=c(rep("visual1",100),rep("visual2",100)) # 100 points of each flavour
Now do:
ggplot(visuals, aes(ISSUE_DATE,COUNTED,group=vis,col=vis)) +
geom_point() + geom_smooth()
and adjust colours etc to taste.
Just combine them. I think this should work but it's untested:
p <- ggplot(visual1, aes(ISSUE_DATE,COUNTED)) + geom_point() +
geom_smooth(fill="blue", colour="darkblue", size=1)
p <- p + geom_point(data=visual2, aes(ISSUE_DATE,COUNTED)) +
geom_smooth(data=visual2, fill="red", colour="red", size=1)
print(p)
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")