【问题标题】:ridgeline plot with frequencies (count) instead of density, on second y-axis and rectangle background在第二个 y 轴和矩形背景上使用频率(计数)而不是密度的山脊线图
【发布时间】:2022-06-11 19:27:12
【问题描述】:

我想使用 ggplot2ggridges 将第二个 y 轴上的频率添加到脊线图

我找到了一个使用 geom_text (https://rdrr.io/cran/ggridges/man/stat_binline.html) 将频率添加为数字的教程,但是,我更愿意将它们添加为第二个 y 轴。

当然,我非常感谢 ggridges 之外的解决方案以获得类似的情节。

示例数据:

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")

reprex package (v2.0.1) 于 2022-06-03 创建

期望的输出:

【问题讨论】:

  • 你的数据点太少了,我几乎看不出密度图的用处...... - 或者你有更多的数据点,你想分享更接近它的样本数据? (也许使用?geom_density_ridges的例子?
  • 谢谢。是的,真实数据有更多的数据点。我认为样本数据应该很小,并且我使用的是长数据格式。因此,我给出了最能代表我的数据结构的示例数据。

标签: r ggplot2 ggridges


【解决方案1】:

从技术上讲,您实际上并没有第二个 y 轴 - 您只想显示频率而不是密度。您通常可以通过使用..count.. 或使用更新的语法after_stat(count) 作为您的审美来显示频率。 ggridges 似乎没有算作计算的统计数据 - 因此可能会伪造您的 ggridges 外观。

示例改编自 ?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") 

reprex package (v2.0.1) 于 2022-06-03 创建

如果你更进一步,让刻面重叠(这是脊图的原理:密度图的重叠刻面),你会发现通过在经典脊图上添加轴指南,将是脊(你的小平面)之间的那些指南的重叠。这看起来不太好。

这与你的 stat 无关,而且 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)

要添加所需的矩形注释,您的方法非常好。数据实际上是否像您的示例中那样结构化,或者您是否刚刚基于第一个框架预先创建了第二个框架? (这样做会很棒而且做得很好)

代码中还有一些 cmets

   
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") 

【讨论】:

  • 谢谢。我尝试获得直方图分布,因此我使用了 stat = "binline" 。也许我可以将stat_bingeom_rect 结合起来,得到一个类似于所需输出的图(即几何上的直方图分布)。
  • @ava 还请注意,如果您有一个在经典岭图上显示您的值的轴指南,那么这些刻面之间的比例将会重叠 - 这看起来会非常混乱。因此,我建议在这种情况下保持各个方面分开
  • 谢谢。我明白你关于经典山脊情节的观点。我尝试使用直方图而不是密度图来表示分布。您的方法可以与geom_rect 结合使用,如上面所需的输出所示?
  • @ava 抱歉,我不在家 - 您在实施 geom rect 方面还需要帮助吗?它只是显示你的价值观在哪里,或者这意味着什么?
  • nob 问题。不幸的是我没有完成它。是的,它只是突出显示时间跨度(第一次到最后一次出现)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 2015-09-13
  • 2019-04-05
  • 2020-02-05
  • 1970-01-01
  • 2015-02-21
相关资源
最近更新 更多