如何实现“ geom_density_ridges”功能

时间:2019-06-08 13:26:02

标签: r ggplot2 density-plot

我想用我的数据生成类似此虹膜数据集所示的图。

ggplot(iris, aes(x = Sepal.Length, y = Species)) +
  geom_density_ridges(aes(fill = Species)) +
  scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))

在这里您可以从我的数据集中看到一个切口。

ratio = c(0, 0.05, 0.1, 0.15, 0.2, 0.25, 0, 0.05, 0.1, 0.15, 0.2, 0.25)
frequency = c(12000, 12300, 9000, 4300, 2434, 18000, 11000, 12200, 8000, 4100, 2400, 15900)
concentration = c("200", "200", "200", "200", "200", "200", "100", "100", "100", "100", "100", "100")
df = cbind(ratio, frequency, concentration)
View(df)
df = as.data.frame(df)

ggplot(df, aes(x = ratio, y = frequency)) +
  geom_density_ridges(aes(fill = df$concentration)) +
  scale_fill_manual(values = c("#00AFBB", "#E7B800"))

很遗憾,我的代码无法正常工作。我不知道我的错误在哪里。

1 个答案:

答案 0 :(得分:0)

这是你的追求吗?

library(ggplot2); library(ggridges)
ggplot(df, aes(x = ratio, y = concentration, 
               height = frequency/10000, fill = concentration)) +
  geom_ridgeline() +
  scale_fill_manual(values = c("#00AFBB", "#E7B800"))

enter image description here

数据:

df = data.frame(stringsAsFactors = F,
                ratio = c(0, 0.05, 0.1, 0.15, 0.2, 0.25, 0, 0.05, 0.1, 0.15, 0.2, 0.25),
                frequency = c(12000, 12300, 9000, 4300, 2434, 18000, 11000, 12200, 8000, 4100, 2400, 15900),
                concentration = c("200", "200", "200", "200", "200", "200", "100", "100", "100", "100", "100", "100"))