我正在尝试制作一系列这样的图形:
我有一些分类和连续数据。当只有分类变量或只有连续变量时,我能够制作这一系列图形。但是,当同时存在两种类型的变量时,我无法生成这一系列图。
我在下面创建了一些数据。有没有一种方法可以调试此代码,以便生成一系列图形?
library(ggplot2)
library(gridExtra)
library(tidyr)
/create some data/
var_1 <- rnorm(100,1,4)
var_2 <- sample( LETTERS[1:2], 100, replace=TRUE, prob=c(0.3, 0.7) )
var_3 <- sample( LETTERS[1:5], 100, replace=TRUE, prob=c(0.2, 0.2,0.2,0.2, 0.1) )
cluster <- sample( LETTERS[1:4], 100, replace=TRUE, prob=c(2.5, 2.5, 2.5, 2.5) )
/put in a frame/
f <- data.frame(var_1, var_2, var_3, cluster)
/convert to factors/
f$var_2 = as.factor(f$var_2)
f$var_3 = as.factor(f$var_3)
f$cluster = as.factor(f$cluster)
/create graphs/
f2 %>% pivot_longer(cols = contains("var"), names_to = "variable") %>%
ggplot(aes(x = value, fill = value)) +
geom_bar() + geom_density() +
facet_grid(rows = vars(cluster),
cols = vars(variable),
scales = "free") +
labs(y = "freq", fill = "Var")
当我只有分类变量时,以下代码有效:
var_2 <- sample( LETTERS[1:2], 100, replace=TRUE, prob=c(0.3, 0.7) )
var_3 <- sample( LETTERS[1:5], 100, replace=TRUE, prob=c(0.2, 0.2,0.2,0.2, 0.1) )
cluster <- sample( LETTERS[1:4], 100, replace=TRUE, prob=c(2.5, 2.5, 2.5, 2.5) )
f <- data.frame(var_2, var_3, cluster)
f$var_2 = as.factor(f$var_2)
f$var_3 = as.factor(f$var_3)
f$cluster = as.factor(f$cluster)
f%>% pivot_longer(cols = contains("var"), names_to = "variable") %>% ggplot(aes(x = value, fill = value)) + geom_bar() + geom_density() +facet_grid(rows = vars(cluster), cols = vars(variable), scales = "free") + labs(y = "freq", fill = "Var")
This is possible to do entirely within ggplot, but it's pretty hacky. Facets are really a way of showing extra dimensions of the same data set. They are not intended to be a way of arbitrarily stitching different plots together, so an entirely ggplot-based solution requires manipulating your data and the axis labels to produce the appearance of stitching plots together.
First, we get the unique levels of the barplot variables as character strings:
levs <- sort(unique(c(as.character(f$var_2), as.character(f$var_3))))
Now, we convert the factors to numbers:
f$var_2 <- as.numeric(factor(f$var_2, levs)) + ceiling(max(f$var_1)) + 10
f$var_3 <- as.numeric(factor(f$var_3, levs)) + ceiling(max(f$var_1)) + 10
We will now construct the breaks and labels that we will use for our x axis
breaks <- c(pretty(range(f$var_1)), sort(unique(c(f$var_2, f$var_3))))
labs <- c(pretty(range(f$var_1)), levs)
Now we can safely pivot our data frame:
f <- pivot_longer(f, cols = c("var_1", "var_2", "var_3"))
对于我们的绘图,我们将使用数据框中的适当子集组进行密度绘图和条形图绘制。然后,我们使用自由标度作为方面,并使用预定义的中断和标签来标记x轴:
ggplot(f, aes(x = value)) +
geom_density(data = subset(f, name == "var_1")) +
geom_bar(data = subset(f, name != "var_1"), aes(fill = name)) +
facet_wrap(cluster~name, ncol = 3, scales = "free") +
scale_x_continuous(breaks = breaks, labels = labs) +
scale_fill_manual(values = c("deepskyblue4", "gold"), guide = guide_none())
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句