first, sorry for my English and mistakes.
I have a plot like this:
data <- data.frame(site=rep(letters[1:6],each=3), year=rep(2001:2003, 6), nb=round(runif(18, min=20, max=60)), group=c(rep("A",9),rep("B", 6),rep("C",3)))
ggplot(data=data, aes(x= factor(year), y= nb)) +
geom_point() +
facet_wrap(~site)
And I would like to add the other panel "group". In fact I would like to make this graph without empty parts:
ggplot(data=data, aes(x= factor(year), y= nb)) +
geom_point() +
facet_grid(group~site)
Does someone has an idea? Thanks for you help!
#
There is this solution which look like that I want, but I thought there were more simple solution :
plt1 <- ggplot(data=data[data$group=="A",], aes(x= factor(year), y= nb)) +
geom_point() +
ggtitle("A")+
facet_grid(~site)+
xlab("") + ylab("")
plt2 <- ggplot(data=data[data$group=="B",], aes(x= factor(year), y= nb)) +
geom_point() +
ggtitle("B")+
facet_grid(~site)+
xlab("") + ylab("")
plt3 <- ggplot(data=data[data$group=="C",], aes(x= factor(year), y= nb)) +
geom_point() +
ggtitle("C")+
facet_grid(~site)+
xlab("") + ylab("")
library(gridExtra)
grid.arrange(arrangeGrob(plt1,plt2, plt3),
left = textGrob("nb",rot=90))
You can combine the site and group inside the facet_wrap() - so you will have only "full" facets.
ggplot(data=data, aes(x= factor(year), y= nb)) +
geom_point() +
facet_wrap(~site+group)
Related
I have a ggplot bar and don't know how to change the scale of the x axis. At the moment it looks like on the image below. However I'd like to reorder the scale of the x axis so that 21% bar is higher than the 7% bar. How could I get the % to the axis? Thanks in advance!
df= data.frame("number" = c(7,21), "name" = c("x","y"))
df
ggplot(df, aes(x=name, y=number)) +
geom_bar(stat="identity", fill = "blue") + xlab("Title") + ylab("Title") +
ggtitle("Title")
Use the prop.table function to in y variable in the geom plot.
ggplot(df, aes(x=name, y=100*prop.table(number))) +
geom_bar(stat="identity", fill = "blue") +
xlab("Stichprobe") + ylab("Paketmenge absolut") +
ggtitle("Menge total")
If you want to have the character, % in the y axis, you can add scale_y_continuous to the plot as below:
library(scales)
ggplot(df, aes(x=name, y=prop.table(number))) +
geom_bar(stat="identity", fill = "blue") +
xlab("Stichprobe") + ylab("Paketmenge absolut") +
ggtitle("Menge total") +
scale_y_continuous(labels=percent)
The only way I am able to duplicate the original plot is, as #sconfluentus noted, for the 7% and 21% to be character strings. As an aside the data frame column names need not be quoted.
df= data.frame(number = c('7%','21%'), name = c("x","y"))
df
ggplot(df, aes(x=name, y=number)) +
geom_bar(stat="identity", fill = "blue") + xlab("Title") + ylab("Title") +
ggtitle("Title")
Changing the numbers to c(0.07, 0.21) and adding, as #Mohanasundaram noted, scale_y_continuous(labels = scales::percent) corrects the situation:
To be pedantic using breaks = c(0.07, 0.21) creates nearly an exact duplicate. See also here.3
Hope this is helpful.
library(ggplot2)
library(scales)
df= data.frame(number = c(0.07,0.21), name = c("KG","MS"))
df
ggplot(df, aes(x=name, y=number)) +
geom_bar(stat="identity", fill = "blue") + xlab("Title") + ylab("Title") +
ggtitle("Title") + scale_y_continuous(labels = scales::percent, breaks = c(.07, .21)))
I have three dataset, they are three distinguished bird's assemblages and I'm trying to merge the test results in one plot together. Each one looks like this:
Exemple:
library(ggplot2)
beta1<-c(0.714286,0.625,0.72973,0.5625,0.733333,1,0.655172,0.92,0.769231,0.586207,0.724138,0.846154,
0.833333,0.76,1)
group<-rep(c("q0", "q1", "q2"), each = 5)
beta2<-c(1.714286,1.625,1.72973,1.5625,1.733333,1,1.655172,1.92,1.769231,1.586217,1.724138,1.846154,
1.833333,1.76,1)
dados1<-data.frame(beta1, group)
dados2<-data.frame(beta2, group)
p1<-ggplot(data=dados1, aes(x=group, y=beta1)) +
stat_summary(fun.y=mean, geom="line", aes(group=1)) +
stat_summary(fun.y=mean, geom="point")+ylim(0,2)
p2<-ggplot(data=dados2, aes(x=group, y=beta2)) +
stat_summary(fun.y=mean, geom="line", aes(group=1)) +
stat_summary(fun.y=mean, geom="point")+ylim(0,2)
the result that I need is like this:
plot_merged
I could do this:
ggplot() + stat_summary(fun.y=mean, geom="line", data=dados2, aes(x=group, y=beta2)) + stat_summary(fun.y=mean, geom="point", data=dados2, aes(x=group, y=beta2)) + stat_summary(fun.y=mean, geom="line", data=dados1, aes(x=group, y=beta1)) + stat_summary(fun.y=mean, geom="point", data=dados1, aes(x=group, y=beta1))+ylim(0,2)
but still not enough, because couldn't plot lines...
So I think this will approximately give what you want. We just combine the beta1 and beta2 in 1 data.frame and plot that:
dados1 <-data.frame(beta = beta1, group, id = "beta1")
dados2 <-data.frame(beta = beta2, group, id = "beta2")
df <- rbind(dados1, dados2)
ggplot(df, aes(group, beta, colour = id, group = id)) +
stat_summary(fun.y=mean, geom="line") +
stat_summary(fun.y=mean, geom="point") +
ylim(0,2)
In addition, I solve other question of mine: "If I want to do the same but with boxplot, how can I do this?"
I tried this:
1.Step - by teunbrand (answer above).
dados1 <-data.frame(beta = beta1, group, id = "beta1")
dados2 <-data.frame(beta = beta2, group, id = "beta2")
df <- rbind(dados1, dados2)
2.step:
ggplot(df, aes(group, beta, colour = id, facet_grid= id))+ geom_boxplot()
Plot
Thank you guys!
Is there a way to add a line for specific factor levels in ggplot?
this simple example could provide a base to explain what I'm trying to say. In this case I'd like to avoid plotting the last level.
ggplot(BOD, aes(x=factor(Time), y=demand, group=1)) + geom_line() + geom_point()
You can just simply create a new variable with an NA-value for Time == 7:
BOD$demand2[BOD$Time<7] <- BOD$demand[BOD$Time<7]
and then plot:
ggplot(BOD, aes(x=factor(Time), y=demand2, group=1)) +
geom_line() +
geom_point() +
theme_classic()
You could also do it on the fly by utilizing the functionality of the data.table-package:
library(data.table)
ggplot(data = as.data.table(BOD)[Time==7, demand := NA],
aes(x=factor(Time), y=demand, group=1)) +
geom_line() +
geom_point() +
theme_classic()
To answer your comment, you could include the point at 7 as follows:
ggplot(BOD, aes(x=factor(Time), y=demand2, group=1)) +
geom_line() +
geom_point(aes(x=factor(Time), y=demand)) +
theme_classic()
In
library(ggplot2)
library(reshape)
df <- as.data.frame(matrix(runif(9),3,3))
df$factor <- letters[1:3]
df.m <- melt(df)
ggplot(df.m, aes(variable, value)) +
geom_boxplot() +
facet_wrap(~factor)
i want to change the facet names. According to the ggplot2 tutorials, this is working:
new.lab <- as_labeller(c(a="A",b="B",c="C"))
ggplot(df.m, aes(variable, value)) +
geom_boxplot() +
facet_wrap(~factor, labeller=new.lab)
However, this is not:
new.lab <- as_labeller(c(a="A",b="B",c=expression(italic("C"))))
ggplot(df.m, aes(variable, value)) +
geom_boxplot() +
facet_wrap(~factor, labeller=new.lab)
How can i get italics (or any other special symbol) in ggplot2 2.0 facets?
How about label_parsed instead?
df.m$f2 <- factor(df.m$factor, labels = c("AAA", "bold(BBB)", "italic(CCC)"))
ggplot(df.m, aes(variable, value)) +
geom_boxplot() +
facet_wrap(~f2, labeller = label_parsed) +
theme(text = element_text(size = 20))
You can specify the type of labeller here using label_parsed,
new.lab <- as_labeller(c(a="A", b="B", c="italic(C)"), label_parsed)
ggplot(df.m, aes(variable, value)) +
geom_boxplot() +
facet_wrap(~factor, labeller = new.lab)
Currently my regression plot looks like this. Notice that
the regression line is deeply buried.
Is there any way I can modify my code here, to show it on top of the dots?
I know I can increase the size but it's still underneath the dots.
p <- ggplot(data=my_df, aes(x=x,y=y),) +
xlab("x") +
ylab("y")+
geom_smooth(method="lm",se=FALSE,color="red",formula=y~x,size=1.5) +
geom_point()
p
Just change the order:
p <- ggplot(data=my_df, aes(x=x,y=y),) +
xlab("x") +
ylab("y")+
geom_point() +
geom_smooth(method="lm",se=FALSE,color="red",formula=y~x,size=1.5)
p
The issue is not the color, but the order of the geoms.
If you first call geom_point() and then geom_smooth()
the latter will be on top of the former.
Plot the following for comparison:
Before <-
ggplot(data=my_df, aes(x=x,y=y),) +
xlab("x") +
ylab("y")+
geom_smooth(method="lm",se=FALSE,color="red",formula=y~x,size=1.5) +
geom_point()
After <-
ggplot(data=my_df, aes(x=x,y=y),) +
xlab("x") +
ylab("y")+
geom_point() +
geom_smooth(method="lm",se=FALSE,color="red",formula=y~x,size=1.5)
How about transparent points?
library(ggplot2)
seed=616
x1<- sort(runif(rnorm(1000)))
seed=626
x2<- rnorm(1000)*0.02+sort(runif(rnorm(1000)))
my_df<- data.frame(x= x1, y = x2)
p <- ggplot(data=my_df, aes(x=x,y=y),) +
xlab("x") +
ylab("y")+
geom_smooth(method="lm",se=FALSE,color="red",formula=y~x,size=1.5)+
geom_point(size = I(2), alpha = I(0.1))
p