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!
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)))
Assuming I have two data.frames with different data but in the same range of x-values
a <-data.frame(x=c(1,1,1,2,2,2,3,3,3),
y=c(0.3,0.4,0.3,0.2,0.5,0.3,0.4,0.4,0.2),
z=c("do","re","mi","do","re","mi","do","re","mi"))
b <- data.frame(x=c(1,2,3),y=c(10,15,8))
Both, a and b have the same range of X values (1,2,3) but while a is a data.frame with 9 rows, b is a data.frame with 3 rows.
I use geom_bar in order to plot the distribution of values of a, like this:
ggplot(a, aes(x=x, y=y, fill=z)) +
geom_bar(position="stack",stat="identity") +
ylab("") +
xlab("x")
And I use geom_line to plot b data, like this:
ggplot(b, aes(x=x, y=y)) +
geom_line(stat="identity") +
ylab("") + xlab("x") + ylim(0,15)
Now I would like to overlay this geom_line plot to the previous geom_bar plot. My first try was to do the following:
ggplot(a, aes(x=x, y=y, fill=z)) +
geom_bar(position="stack",stat="identity") +
ylab("") + xlab("x") +
ggplot(b, aes(x=x, y=y)) +
geom_line(stat="identity") +
ylab("") + xlab("x") + ylim(0,15)
With no success.
How can I overlay a geom_line plot to a geom_bar plot?
Try this
p <- ggplot()
p <- p + geom_bar(data = a, aes(x=x, y=y, fill=z), position="stack",stat="identity")
p <- p + geom_line(data = b, aes(x=x, y=y/max(y)), stat="identity")
p
Update:
You can rescale the one y to make them the same. As I don't know the relations between the two ys, I rescaled them by using y/max(y). Does this solve you problem?
Try merging the datasets first, then plotting, like this:
require(ggplot2)
df <- merge(a,b,by="x")
ggplot(df, aes(x=x, y=y.x, fill=z)) +
geom_bar(position="stack",stat="identity") +
geom_line(aes(x=x, y=y.y)) +
ylab("") + xlab("x")
Output:
I edited the sample data to better illustrate the effects, because the y-axis scaling of the original data would not have matched well:
a <-data.frame(x=c(1,1,1,2,2,2,3,3,3),
y=c(0.3,0.4,0.3,0.2,0.5,0.3,0.4,0.4,0.2),
z=c("do","re","mi","do","re","mi","do","re","mi"))
b <- data.frame(x=c(1,2,3),y=c(.4,1,.4))
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)
So I have this data:
dataset = rbinom(1000, 16, 0.5)
mean = mean(dataset)
sd = sd(dataset)
data_subset = subset(dataset, dataset >= (mean - 2*sd) & dataset <= (mean + 2*sd))
dataset = data.frame(X=dataset)
data_subset = data.frame(X=data_subset)
And here's how I'm drawing my histogram for dataset:
ggplot(dataset, aes(x = X)) +
geom_histogram(aes(y=..density..), binwidth=1, colour="black", fill="white") +
theme_bw()
How can I shade the data_subset portion of the histogram, like so?
My solution is very similar to joran's -- I think they're both worth looking at for the slight differences:
ggplot(dataset,aes(x=X)) +
geom_histogram(binwidth=1,fill="white",color="black") +
geom_histogram(data=subset(dataset,X>6&X<10),binwidth=1,
colour="black", fill="grey")+theme_bw()
Just add another geom_histogram line using that data subset (although you may have to tinker with the binwidth a bit, I'm not sure):
ggplot(dataset, aes(x = X)) +
geom_histogram(aes(y=..density..), binwidth=1, colour="black", fill="white") +
geom_histogram(data = data_subset,aes(y=..density..), binwidth=1, colour="black",fill = "grey") +
theme_bw()
The last image in this blog post.
I have tried searching "nested bar graph" and "hierarchical bar graph", but they may not be the word for it.
Use ggplot and create separate layers:
library(ggplot2)
set.seed(1)
stupid <- data.frame(
group= LETTERS[1:5],
men = sample(1:10, 5),
women = sample(1:10, 5)
)
# Melt the data and calculate totals
mstupid <- melt(stupid, id.vars="group")
stupidTotal <- ddply(mstupid, .(group), summarize, value=sum(value))
ggplot() +
geom_bar(data=stupidTotal, aes(x=group, y=value), fill="grey50") +
geom_bar(data=mstupid, aes(x=group, y=value, fill=variable),
stat="identity", position="dodge") +
theme_bw()
Look for 'barNest' in package plotrix
Use this:
ggplot() +
geom_bar(data=stupidTotal, aes(x=group, y=value, fill="grey50"), stat="identity") +
geom_bar(data=mstupid, aes(x=group, y=value, fill=variable),
stat="identity", position="dodge") +
theme_bw()