Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

r - multiple histograms with ggplot2 - position

I am trying to plot side by side the following datasets

dataset1=data.frame(obs=runif(20,min=1,max=10))
dataset2=data.frame(obs=runif(20,min=1,max=20))
dataset3=data.frame(obs=runif(20,min=5,max=10))
dataset4=data.frame(obs=runif(20,min=8,max=10))

I've tried to add the option position="dodge" for geom_histogram with no luck. How can I change the following code to plot the histograms columns side by side without overlap ??

ggplot(data = dataset1,aes_string(x = "obs",fill="dataset")) +
geom_histogram(binwidth = 1,colour="black", fill="blue")+
geom_histogram(data=dataset2, aes_string(x="obs"),binwidth = 1,colour="black",fill="green")+
geom_histogram(data=dataset3, aes_string(x="obs"),binwidth = 1,colour="black",fill="red")+
geom_histogram(data=dataset4, aes_string(x="obs"),binwidth = 1,colour="black",fill="orange")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ggplot2 works best with "long" data, where all the data is in a single data frame and different groups are described by other variables in the data frame. To that end

DF <- rbind(data.frame(fill="blue", obs=dataset1$obs),
            data.frame(fill="green", obs=dataset2$obs),
            data.frame(fill="red", obs=dataset3$obs),
            data.frame(fill="orange", obs=dataset3$obs))

where I've added a fill column which has the values that you used in your histograms. Given that, the plot can be made with:

ggplot(DF, aes(x=obs, fill=fill)) +
  geom_histogram(binwidth=1, colour="black", position="dodge") +
  scale_fill_identity()

where position="dodge" now works.

enter image description here

You don't have to use the literal fill color as the distinction. Here is a version that uses the dataset number instead.

DF <- rbind(data.frame(dataset=1, obs=dataset1$obs),
            data.frame(dataset=2, obs=dataset2$obs),
            data.frame(dataset=3, obs=dataset3$obs),
            data.frame(dataset=4, obs=dataset3$obs))
DF$dataset <- as.factor(DF$dataset)
ggplot(DF, aes(x=obs, fill=dataset)) +
  geom_histogram(binwidth=1, colour="black", position="dodge") +
  scale_fill_manual(breaks=1:4, values=c("blue","green","red","orange"))

This is the same except for the legend.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...