简体   繁体   English

山脊线 plot,频率(计数)而不是密度,在第二个 y 轴和矩形背景上

[英]ridgeline plot with frequencies (count) instead of density, on second y-axis and rectangle background

I would like to add frequencies on a second y-axis to a ridgeline plot using ggplot2 and ggridges我想使用ggplot2ggridges

I found a tutorial adding the frequencies as numbers with geom_text ( https://rdrr.io/cran/ggridges/man/stat_binline.html ), however, I would prefer to add them as a second y-axis.我找到了一个使用geom_text ( https://rdrr.io/cran/ggridges/man/stat_binline.html ) 添加频率作为数字的教程,但是,我更愿意将它们添加为第二个 y 轴。

Of course I very appreciate solutions outside ggridges to get a similar plot.当然,我非常感谢ggridges之外的解决方案,以获得类似的 plot。

Example data:示例数据:

library(ggplot2)
library(ggridges)
library(lubridate)

# datapoints
data_timepoint <- data.frame(type=factor(c("A","B","C","D")),
                             start=as.Date(c("1990-01-01","2000-01-01","2010-01-01","2012-01-01")),
                             stop=as.Date(c(rep("2022-01-01",4))))

                             
                             
# frequencies                             
data_freq <- data.frame(type=c("A","A","B","C","D","D","D"),
                        year=ymd(year(as.Date(c("1991-01-01","1991-01-01","2005-01-01","2016-01-01","2013-01-01","2013-01-01","2015-01-01"))),truncated=2L))
                                 




# plot
ggplot(data_timepoint) +
  geom_rect(aes(xmin=start, xmax=stop,
                ymin=type, ymax=as.numeric(type)+0.9), fill="lightblue") +
  geom_density_ridges(data=data_freq, aes(x=year,y=type),stat = "binline",
                      bins = 1, scale = 0.95, draw_baseline = FALSE, alpha=.5, binwidth=10,center=20) +
  scale_x_date(date_breaks = "1 year",date_labels = "%Y") +
    theme(axis.text.x = element_text(angle = 90),
        axis.text.y = element_text(vjust = -2)) +
  labs(title="",y="Type",x="Year")

Created on 2022-06-03 by the reprex package (v2.0.1)reprex package (v2.0.1) 创建于 2022-06-03

Desired output:所需的 output: 在此处输入图像描述

You technically don't really have a secondary y axis - you just want to show frequency instead of density.从技术上讲,您实际上并没有辅助 y 轴 - 您只想显示频率而不是密度。 You can generally show frequency by using ..count.. or with newer syntax after_stat(count) as your y aesthetic.您通常可以通过使用..count..或更新的语法after_stat(count)作为您的审美来显示频率。 ggridges doesn't seem to have count as a computed stat - therefore maybe fake your ggridges look with facets. ggridges 似乎没有算作计算统计数据 - 因此可能会伪造你的 ggridges 外观。

The example is adapted from?geom_density_ridges例子改编自?geom_density_ridges

library(ggplot2)

## swap x and y
ggplot(diamonds, aes(price)) +
## use y = after_stat(count) to show your frequency
  geom_density(aes(y =after_stat(count))) +
## change the y axis position to the right
  scale_y_continuous(expand = c(0.01, 0), position = "r") +
  scale_x_continuous(expand = c(0.01, 0)) +
## add facet, and put label to the left
  facet_wrap(~cut, ncol = 1, strip.position = "l") 

Created on 2022-06-03 by the reprex package (v2.0.1)reprex package (v2.0.1) 创建于 2022-06-03

If you go a step further, and let the facets overlap (which is the principle of a ridge plot: overlapping facets of a density plot), you will see that by adding an axis guide to a classic ridge plot, there will be overlap of those guides between the ridges (your facets).如果你 go 更进一步,让面重叠(这是脊线的原理 plot:密度图的重叠面),你会看到通过向经典脊线 plot 添加轴指南,将有重叠脊之间的那些指南(你的小平面)。 This doesn't look good.这看起来不太好。

This is irrespective of your stat, and will also happen with stat = "binline"这与您的统计数据无关,并且在 stat = "binline" 时也会发生

p <- ggplot(diamonds, aes(price)) +
  geom_density(aes(y = ..count..)) +
  scale_y_continuous(expand = c(0.01, 0), position = "r") +
  scale_x_continuous(expand = c(0.01, 0)) +
  facet_wrap(~cut, ncol = 1, strip.position = "l")  +
## let the facets overlap (make background and strip transparent)
  theme(panel.spacing.y = unit(-.3, "in"), 
        strip.background = element_blank(),
        panel.background = element_blank(), 
        panel.grid.major = element_blank())

cowplot::stamp_bad(p)

To add your desired rectangle annotation, your approach is perfectly fine.要添加所需的矩形注释,您的方法非常好。 Is the data actually structured as in your example or have you just created the second frame beforehand based on the first one?数据实际上是按照您的示例构建的,还是您只是根据第一帧预先创建了第二帧? (This would be excellent and well done doing so) (这将是非常好的,并且做得很好)

Few more comments in the code代码中的更多注释

   
ggplot() +
## use different y - slightly depending on your desired look 
geom_rect(data = data_timepoint, aes(xmin=start, xmax=stop, ymin=0, ymax=1), fill="lightblue") +
geom_histogram(data= data_freq, aes(year)) +
## added pretty labels
scale_y_continuous(expand = c(0, 0), position = "r", breaks = scales::breaks_pretty(n = 2)) +
## keep x as date
scale_x_date(expand = c(0, 0)) +
facet_wrap(~type, ncol = 1, strip.position = "l") 

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM