多个直方图与ggplot2 - 位置 [英] multiple histograms with ggplot2 - position
问题描述
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))
我试图为geom_histogram添加选项 position =dodge,但没有运气。
ggplot(data = dataset1,aes_string)如何更改以下代码以并排绘制直方图列? (x =obs,fill =dataset))+
geom_histogram(binwidth = 1,color =black,fill =blue)+
geom_histogram(data = dataset2,aes_string( x =obs),binwidth = 1,color =black,fill =green)+
geom_histogram(data = dataset3,aes_string(x =obs),binwidth = 1,color =黑色,填充=红色)+
geom_histogram(data = dataset4,aes_string(x =obs),binwidth = 1,color =black,fill =orange)
ggplot2最适合长数据,其中所有数据都在单个数据帧和不同的组由数据帧中的其他变量描述。为此
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))
我添加了一个 fill
列,其中包含您在直方图中使用的值。假设情节可以用下面的方式表示:
$ g $ p $ ggplot(DF,aes(x = obs,fill = fill))+
geom_histogram(binwidth = 1,color =black,position =dodge)+
scale_fill_identity()
其中 position =dodge
现在可以使用。
您不必使用文字填充颜色作为区分。
DF < - rbind(data.frame(dataset = 1,obs = dataset1 $ obs),
data.frame(dataset = 2,obs = dataset2 $ obs),
data.frame(dataset = 3,obs = dataset3 $ obs),
数据。 frame(dataset = 4,obs = dataset3 $ obs))
DF $ dataset< - as.factor(DF $ dataset)
ggplot(DF,aes(x = obs,fill = dataset)) +
geom_histogram(binwidth = 1,color =black,position =dodge)+
scale_fill_manual(breaks = 1:4,values = c(blue,green,red ,orange))
除了图例外, b
$ b
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")
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.
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.
这篇关于多个直方图与ggplot2 - 位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!