Multiple graphs with different x-axis ticks [duplicate] - r

This question already has answers here:
Order discrete x scale by frequency/value
(7 answers)
Closed 6 years ago.
I have the following data.frame:
ef2 <- data.frame(X1=c(50,100,'bb','aa'), X2=c('A','A','B','B'), value=c(1,4,3,6))
I want to create two plots, one for each group in X2.
Here is the code I have and the plot obtained:
ggplot(data=ef2, aes(x=X1, y=value, group=X2)) +
facet_grid(.~X2, scales="free_x") +
geom_line(size=1) +
geom_point(size=3) +
xlab('') +
ylab('Y')
The problem is that the x-axis is ordered alphabetically and I don't know how to fix it. I have tried adding scale_x_discrete, but I don't know how to separate groups. You can see the plot I obtained adding this parameter in the following link:
ggplot(data=ef2, aes(x=X1, y=value, group=X2)) +
facet_grid(.~X2, scales="free_x") +
geom_line(size=1) +
geom_point(size=3) +
xlab('') +
ylab('Y') +
scale_x_discrete(limits=ef2$X1)
Edited: I can't change ef2 data.frame. I've tried ordering factors in another data.frame:
ef2 <- data.frame(X1=c(50,100,'bb','aa'), X2=c('A','A','B','B'), value=c(1,4,3,6))
ef2$X1 <- as.character(ef2$X1)
nou <- data.frame(X1=factor(ef2$X1), levels=ef2$X1, X2=ef2$X2, value=ef2$value)
But it doesn't work.

This worked for me but I am not sure if it is exactly what you need:
ef2 <- data.frame(X1=factor(c('50','100','bb','aa'), levels = c('50','100','bb','aa')), X2=c('A','A','B','B'), value=c(1,4,3,6))
ggplot(data=ef2, aes(x=X1, y=value, group=X2)) +
facet_grid(.~X2, scales="free_x") +
geom_line(size=1) +
geom_point(size=3) +
xlab('') +
ylab('Y')
According to this post: Avoid ggplot sorting the x-axis while plotting geom_bar()
ggplot orders automatically unless you provide an already orderd factor.
Update:
The code you use has an error. levels is an argument of the factor function.
Try this:
ef2 <- data.frame(X1=c(50,100,'bb','aa'), X2=c('A','A','B','B'), value=c(1,4,3,6))
ef2$X1 <- factor(ef2$X1, levels = unique(ef2$X1))

Related

Creating a ggplot boxplot with jitter duplicates my data (R) [duplicate]

This question already has answers here:
R ggplot geom_jitter duplicates outlier
(1 answer)
How to exclude outliers when using geom_boxplot() + geom_jitter() in R
(2 answers)
Closed last year.
I want to create a boxplot that shows individual data points as well.
This is the code that I am using:
ggplot(data, aes(x=treatment, y=aggregate_count, color = treatment)) +
geom_boxplot() +
geom_point(position = "jitter") +
ylab("Aggregate Count") +
xlab("") +
theme_classic()
Using both the geom_boxplot() and geom_point() function together like this however duplicates my dataset. I noticed this because there is only one value in my dataset with a value above 30, but in the plot, I can see two. If I remove either geom_boxplot() or geom_point() the data gets displayed correctly.
Does someone have an idea on how to fix this?
Thank you in advance!!

truncate y-axis, data disappears [duplicate]

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:

Horizontal barplot in ggplot2 with faces having different categories

I have a data frame with ids, categories, and values, while I manage easily to have an horizontal dot-plot, with products grouped by category in distinct facets,
when I try with a barplot, event categories with missing data show up.
Any hint? Is it a bug or am I missing some detail?
Thanks,
Marco.
## I have a data frame with ids, categories, and values
d=data.frame(prd=c("orange","apple","pear","bread","crackers"),
cat=c("fruit","fruit","fruit","bakery","bakery"),
qty=c(10,20,15,8,17)
)
# I manage to have an horizontal dot-plot, with products grouped by category in distinct facets
ggplot(d,aes(y=prd,x=qty)) +
geom_point(stat="identity",size=4) +
geom_segment(aes(yend=prd), xend=0, colour="grey50") +
facet_grid(cat ~ .,scale="free",space="free") +
theme_light()
# though when I try with a barplot, bars, with missing data show up
ggplot(d,aes(x=prd,y=qty)) +
geom_bar(stat="identity") +
coord_flip() +
facet_grid(cat ~ .,scale="free",space="free") +
theme_light()
Ggplot2 does not currently support free scales with a non-cartesian coord or coord_flip.
So either you could plot them without flip:
ggplot(d,aes(y=qty,x=prd)) +
facet_wrap(~cat, scale="free") +
geom_bar(stat="identity") +
theme_light()
Or you flip but use a workaround, for ex. make one categorical variable out of two (I admit that this solution is not very visually appealing):
d$n <- paste(d$cat, d$prd, sep="|")
ggplot(d,aes(y=qty,x=n)) +
geom_bar(stat="identity") +
coord_flip() +
theme_light()

Draw mean and outlier points for box plots using ggplot2

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

ggplot2: how to show the legend [duplicate]

This question already has answers here:
Add legend to ggplot2 line plot
(4 answers)
Closed 2 years ago.
I made a simple classic plot with ggplot2 which is two graphs in one. However, I'm struggling in showing the legend. It's not showing the legend. I didn't use the melt and reshape way, I just use the classic way. Below is my code.
df <- read.csv("testDataFrame.csv")
graph <- ggplot(df, aes(A)) +
geom_line(aes(y=res1), colour="1") +
geom_point(aes(y=res1), size=5, shape=12) +
geom_line(aes(y=res2), colour="2") +
geom_point(aes(y=res2), size=5, shape=20) +
scale_colour_manual(values=c("red", "green")) +
scale_x_discrete(name="X axis") +
scale_y_continuous(name="Y-axis") +
ggtitle("Test")
#scale_shape_discrete(name ="results",labels=c("Res1", "Res2"),solid=TRUE)
print(graph)
the data frame is:
A,res1,res2
1,11,25
2,29,40
3,40,42
4,50,51
5,66,61
6,75,69
7,85,75
Any suggestion on how to show the legend for the above graph?
In ggplot2, legends are shown for every aesthetic (aes) you set; such as group, colour, shape. And to do that, you'll have to get your data in the form:
A variable value
1 res1 11
... ... ...
6 res1 85
7 res2 75
You can accomplish this with reshape2 using melt (as shown below):
require(reshape2)
require(ggplot2)
ggplot(dat = melt(df, id.var="A"), aes(x=A, y=value)) +
geom_line(aes(colour=variable, group=variable)) +
geom_point(aes(colour=variable, shape=variable, group=variable), size=4)
For example, if you don't want colour for points, then just remove colour=variable from geom_point(aes(.)). For more legend options, follow this link.

Resources