无论我尝试什么,我都无法使用ggridges. 使用graphing_dataframe如下所示的数据框:
str(graphing_dataframe)
summary(graphing_dataframe)
> str(graphing_dataframe)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 14 obs. of 3 variables:
$ id : chr "00343" "00343" "00343" "00343" ...
$ week : num 14 1 2 3 4 5 6 7 8 9 ...
$ rating: num 14 4 12 8 14 19 16 16 7 8 ...
- attr(*, "spec")=
.. cols(
.. id = col_character(),
.. week = col_double(),
.. rating = col_double()
.. )
> summary(graphing_dataframe)
id …Run Code Online (Sandbox Code Playgroud) 当我使用 时geom_density_ridges(),该图通常最终会显示数据中不存在的值的长尾。
下面是一个例子:
library(tidyverse)
library(ggridges)
data("lincoln_weather")
# Remove all negative values for "Minimum Temperature"
d <- lincoln_weather[lincoln_weather$`Min Temperature [F]`>=0,]
ggplot(d, aes(`Min Temperature [F]`, Month)) +
geom_density_ridges(rel_min_height=.01)
Run Code Online (Sandbox Code Playgroud)
如您所见,一月、二月和十二月都显示负温度,但数据中根本没有负值。
当然,我可以对 x 轴添加限制,但这并不能解决问题,因为它只是截断了现有的错误密度。
ggplot(d, aes(`Min Temperature [F]`, Month)) +
geom_density_ridges(rel_min_height=.01) +
xlim(0,80)
Run Code Online (Sandbox Code Playgroud)
现在,该图使 1 月和 2 月的值看起来为零(没有)。这也使得 0 度看起来在 12 月经常发生,而实际上只有 1 个这样的日子。
我怎样才能解决这个问题?
使用包geom_density_ridges中的函数时ggridges,它始终为图中的所有密度选择带宽。但是有没有办法调整它选择的带宽呢?
我目前有一些代码可以制作山脊图,但对于底部密度来说带宽太低。我想对其进行调整,使其更平滑、不那么粗糙。
这是我的代码:
hier_plot <- ggplot(hier_df, aes(x=x, y=as.factor(beta), fill = factor(beta))) +
theme(axis.title = element_text(size = 15),
axis.text = element_text(size = 15),
legend.text = element_text(size = 10),
panel.background = element_rect(fill = "#fffffC")) +
labs(y = expression(beta), x = 'x', expression(beta), fill = expression(beta)) +
geom_density_ridges(scale = 2.5) +
scale_x_continuous(expand = c(0.01, 0)) +
scale_y_discrete(expand = c(0.05, 0)) +
scale_fill_brewer(palette = 'Reds')
hier_plot
Run Code Online (Sandbox Code Playgroud) 我想使用ggridges.
# toy example
ggplot(iris, aes(x=Sepal.Length, y=Species, fill=..x..)) +
geom_density_ridges_gradient(jittered_points = FALSE, quantile_lines =
FALSE, quantiles = 2, scale=0.9, color='white') +
scale_y_discrete(expand = c(0.01, 0)) +
theme_ridges(grid = FALSE, center = TRUE)
Run Code Online (Sandbox Code Playgroud)
我想在 7 处为 virginica 添加一条垂直线,为 versicolor 添加 4 条,为 setosa 添加 5 条垂直线。关于如何做到这一点的任何想法?
为什么情节的顶部被切断了,我该如何解决这个问题?我增加了利润率,但没有任何区别。
查看 1854 年的曲线,位于左侧驼峰的最顶端。看起来驼峰顶部的线条更细。对我来说,将大小更改为 0.8 无济于事。
这是生成此示例所需的代码:
library(tidyverse)
library(ggridges)
t2 <- structure(list(Date = c("1853-01", "1853-02", "1853-03", "1853-04",
"1853-05", "1853-06", "1853-07", "1853-08", "1853-09", "1853-10",
"1853-11", "1853-12", "1854-01", "1854-02", "1854-03", "1854-04",
"1854-05", "1854-06", "1854-07", "1854-08", "1854-09", "1854-10",
"1854-11", "1854-12"), t = c(-5.6, -5.3, -1.5, 4.9, 9.8, 17.9,
18.5, 19.9, 14.8, 6.2, 3.1, -4.3, -5.9, -7, -1.3, 4.1, 10, 16.8,
22, 20, 16.1, 10.1, 1.8, -5.6), year = c("1853", "1853", "1853",
"1853", "1853", "1853", "1853", "1853", "1853", "1853", "1853",
"1853", "1854", "1854", "1854", …Run Code Online (Sandbox Code Playgroud) 我正在尝试为ggplot2 中geom_segment的geom_density_ridges绘图添加方法。
library(dplyr)
library(ggplot2)
library(ggridges)
Fig1 <- ggplot(Figure3Data, aes(x = `hairchange`, y = `EffortGroup`)) +
geom_density_ridges_gradient(aes(fill = ..x..), scale = 0.9, size = 1)
ingredients <- ggplot_build(Fig1) %>% purrr::pluck("data", 1)
density_lines <- ingredients %>%
group_by(group) %>% filter(density == mean(density)) %>% ungroup()
p <- ggplot(Figure3Data, aes(x = `hairchange`, y = `EffortGroup`)) +
geom_density_ridges_gradient(aes(fill = ..x..), scale = 0.9, size = 1) +
scale_fill_gradientn( colours = c("#0000FF", "#FFFFFF", "#FF0000"),name =
NULL, limits=c(-2,2))+ coord_flip() +
theme_ridges(font_size = 20, grid=TRUE, …Run Code Online (Sandbox Code Playgroud) 我有一个数据集,作为 CSV 格式的要点上传到这里。它是 YouGov 文章“‘好’有多好?”中提供的 PDF 的提取形式。. 被要求用 0(非常负面)和 10(非常正面)之间的分数对单词(例如“完美”、“糟糕”)进行评分的人。要点正好包含该数据,即对于每个单词(列:单词),它为从 0 到 10(列:类别)的每个排名存储投票数(列:总计)。
我通常会尝试使用 matplotlib 和 Python 来可视化数据,因为我缺乏 R 方面的知识,但似乎 ggridges 可以创建比我使用 Python 所做的更好的绘图。
使用:
library(ggplot2)
library(ggridges)
YouGov <- read_csv("https://gist.githubusercontent.com/camminady/2e3aeab04fc3f5d3023ffc17860f0ba4/raw/97161888935c52407b0a377ebc932cc0c1490069/poll.csv")
ggplot(YouGov, aes(x=Category, y=Word, height = Total, group = Word, fill=Word)) +
geom_density_ridges(stat = "identity", scale = 3)
Run Code Online (Sandbox Code Playgroud)
忽略我必须调整美学的事实,我很难做到三件事:
我试图调整来自这个来源的建议,但最终失败了,因为我的数据似乎格式错误:我已经有了每个类别的汇总投票数,而不是单一的投票实例。
我希望最终得到一个更接近这个情节的结果,它满足标准 3(来源):

我的数据框如下所示:
df <- data.frame(label=c("yahoo","google","yahoo","yahoo","google","google","yahoo","yahoo"), year=c(2000,2001,2000,2001,2003,2003,2003,2003))
Run Code Online (Sandbox Code Playgroud)
如何产生这样的热图:
library(ggplot2)
library(ggridges)
theme_set(theme_ridges())
ggplot(
lincoln_weather,
aes(x = `Mean Temperature [F]`, y = `Month`)
) +
geom_density_ridges_gradient(
aes(fill = ..x..), scale = 3, size = 0.3
) +
scale_fill_gradientn(
colours = c("#0D0887FF", "#CC4678FF", "#F0F921FF"),
name = "Temp. [F]"
)+
labs(title = 'Temperatures in Lincoln NE')
Run Code Online (Sandbox Code Playgroud)
如何翻转绘图轴,即以年份为 x 轴,以 y 轴为标签?