I have two dataframes: dataf1, dataf2. They have the same structure and columns. 3 columns names are A,B,C. And they both have 50 rows. I would like to plot the histogram of column B on dataf1 and dataf2. I can plot two histograms separately but they are not of the same scale. I would like to know how to either put them on the same histogram using different colors or plot two histograms of the same scale?
我有两个数据帧:dataf1,dataf2。它们具有相同的结构和列。 3列名称是A,B,C。它们都有50行。我想在dataf1和dataf2上绘制B列的直方图。我可以分别绘制两个直方图,但它们的尺度不同。我想知道如何使用不同的颜色将它们放在相同的直方图上或绘制两个相同比例的直方图?
ggplot() + aes(dataf1$B)+ geom_histogram(binwidth=1, colour="black",fill="white")
ggplot() + aes(dataf2$B)+ geom_histogram(binwidth=1, colour="black", fill="white")
2 个解决方案
#1
1
As Zheyuan says, you just need to set the y limits for each plot to get them on the same scale. With ggplot2
, one way to do this is with the lims
command (though scale_y_continuous
and coord_cartesian
also work, albeit slightly differently). You also should never use data$column
indside aes()
. Instead, use the data
argument for the data frame and unquoted column names inside aes()
. Here's an example with some built-in data.
正如哲源所说,你只需要为每个情节设定y限制,使它们达到相同的比例。使用ggplot2,一种方法是使用lims命令(虽然scale_y_continuous和coord_cartesian也可以工作,虽然略有不同)。你也不应该使用数据$ column indside aes()。相反,在aes()中使用数据框的数据参数和不带引号的列名。这是一个内置数据的示例。
p1 = ggplot(mtcars, aes(x = mpg)) + geom_histogram() + lims(y = c(0, 13))
p2 = ggplot(iris, aes(x = Sepal.Length)) + geom_histogram() + lims(y = c(0, 13))
gridExtra::grid.arrange(p1, p2, nrow = 1)
Two get two histograms on the same plot, the best way is to combine your data frames. A guess, without seeing what your data looks like:
两个在同一个图上得到两个直方图,最好的方法是组合你的数据帧。猜测,没有看到你的数据是什么样的:
dataf = rbind(dataf1["B"], dataf2["B"])
dafaf$source = c(rep("f1", nrow(dataf1)), rep("f2", nrow(dataf2))
ggplot(dataf, aes(x = B, fill = source)) +
geom_histogram(position = "identity", alpha = 0.7)
#2
1
Combine your data into a single data frame with a new column marking which data frame the data originally came from. Then use that new column for the fill aesthetic for your plot.
将您的数据合并到一个数据框中,并使用新的列标记数据最初来自哪个数据框。然后使用该新专栏为您的情节填充美学。
data1$source="Data 1"
data2$source="Data 2"
dat_combined = rbind(data1, data2)
You haven't provided sample data, so here are a few examples of possible plots, using the built-in iris
data frame. In the plots below, dat
is analogous to dat_combined
, Petal.Width
is analogous to B
, and Species
is analogous to source
.
您尚未提供样本数据,因此以下是使用内置虹膜数据框的可能图表的几个示例。在下图中,dat类似于dat_combined,Petal.Width类似于B,而Species类似于source。
dat = subset(iris, Species != "setosa") # We want just two species
ggplot(dat, aes(Petal.Width, fill=Species)) +
geom_histogram(position="identity", colour="grey40", alpha=0.5, binwidth=0.1)
ggplot(dat, aes(Petal.Width, fill=Species)) +
geom_histogram(position="dodge", binwidth=0.1)
ggplot(dat, aes(Petal.Width, fill=Species)) +
geom_histogram(position="identity", colour="grey40", binwidth=0.1) +
facet_grid(Species ~ .)
#1
1
As Zheyuan says, you just need to set the y limits for each plot to get them on the same scale. With ggplot2
, one way to do this is with the lims
command (though scale_y_continuous
and coord_cartesian
also work, albeit slightly differently). You also should never use data$column
indside aes()
. Instead, use the data
argument for the data frame and unquoted column names inside aes()
. Here's an example with some built-in data.
正如哲源所说,你只需要为每个情节设定y限制,使它们达到相同的比例。使用ggplot2,一种方法是使用lims命令(虽然scale_y_continuous和coord_cartesian也可以工作,虽然略有不同)。你也不应该使用数据$ column indside aes()。相反,在aes()中使用数据框的数据参数和不带引号的列名。这是一个内置数据的示例。
p1 = ggplot(mtcars, aes(x = mpg)) + geom_histogram() + lims(y = c(0, 13))
p2 = ggplot(iris, aes(x = Sepal.Length)) + geom_histogram() + lims(y = c(0, 13))
gridExtra::grid.arrange(p1, p2, nrow = 1)
Two get two histograms on the same plot, the best way is to combine your data frames. A guess, without seeing what your data looks like:
两个在同一个图上得到两个直方图,最好的方法是组合你的数据帧。猜测,没有看到你的数据是什么样的:
dataf = rbind(dataf1["B"], dataf2["B"])
dafaf$source = c(rep("f1", nrow(dataf1)), rep("f2", nrow(dataf2))
ggplot(dataf, aes(x = B, fill = source)) +
geom_histogram(position = "identity", alpha = 0.7)
#2
1
Combine your data into a single data frame with a new column marking which data frame the data originally came from. Then use that new column for the fill aesthetic for your plot.
将您的数据合并到一个数据框中,并使用新的列标记数据最初来自哪个数据框。然后使用该新专栏为您的情节填充美学。
data1$source="Data 1"
data2$source="Data 2"
dat_combined = rbind(data1, data2)
You haven't provided sample data, so here are a few examples of possible plots, using the built-in iris
data frame. In the plots below, dat
is analogous to dat_combined
, Petal.Width
is analogous to B
, and Species
is analogous to source
.
您尚未提供样本数据,因此以下是使用内置虹膜数据框的可能图表的几个示例。在下图中,dat类似于dat_combined,Petal.Width类似于B,而Species类似于source。
dat = subset(iris, Species != "setosa") # We want just two species
ggplot(dat, aes(Petal.Width, fill=Species)) +
geom_histogram(position="identity", colour="grey40", alpha=0.5, binwidth=0.1)
ggplot(dat, aes(Petal.Width, fill=Species)) +
geom_histogram(position="dodge", binwidth=0.1)
ggplot(dat, aes(Petal.Width, fill=Species)) +
geom_histogram(position="identity", colour="grey40", binwidth=0.1) +
facet_grid(Species ~ .)