4

我目前正在使用 ggridges 库,以制作“joychart”。我写了这个:

data3 %>%
  mutate(ftFct = fct_rev(as.factor(ft_newnum))) %>%
  ggplot(aes(y = ftFct)) +
  geom_density_ridges(aes(x = ft, fill = paste(ftFct, rep)), 
                      alpha = .8, color = "white", from = 0, to = 100) +
  labs(x = "Feeling Themometer Responses (%)",
   y = " ",
   title = "Republican vs Democratic Views Towards...",
   subtitle = "Analysis unit: students (n = 595)") +
   scale_y_discrete(expand = c(0.01, 0)) +
   scale_x_continuous(expand = c(0.01, 0)) +
   scale_fill_cyclical(breaks = c("2 0", "2 1"),
                       labels = c(`2 0` = "Democrat", `2 1` = "Republican"),
                       values = c("#8080ff", "#ff8080", "#0000ff", "#ff0000"),
                       name = "Political Affiliation", guide = "legend") +
  theme_ridges(grid = FALSE)

...这让我得到了这个数字:

在此处输入图像描述

这正是我想要的——完美的格式,每行在深色和浅色之间交替,提供一些对比度和提高可读性。

接下来,我标记 y 轴变量,以便我们知道我们在看什么。我这样标记“ft_newnum”:

data3$ft_newnum <- factor(data3$ft_newnum,
                       levels = c(2,3,4,5,6,7,9,11,12, 13, 14, 15),
                       labels = c("Donald Trump", "Christian fundamentalists", "Elites", 'Republicans', 'Denison Republicans', 'Denison Greeks', 'Denison Varsity Athlete','Hillary Clinton', 'Denison Democrats', 'Democrats', 'Bernie Sanders', 'Weinberg'))

然后编辑代码以合并此更改:

data3 %>%
  mutate(ftFct = fct_rev(as.factor(ft_newnum))) %>%
  ggplot(aes(y = ftFct)) +
  geom_density_ridges(aes(x = ft, fill = paste(ftFct, rep)), 
                      alpha = .8, color = "white", from = 0, to = 100) +
  labs(x = "Feeling Themometer Responses (%)",
       y = " ",
       title = "Republican vs Democratic Views Towards...",
       subtitle = "Analysis unit: students (n = 595)") +
  scale_y_discrete(expand = c(0.01, 0)) +
  scale_x_continuous(expand = c(0.01, 0)) +
  scale_fill_cyclical(breaks = c("Donald Trump 0", "Donald Trump 1"),
                      labels = c(`Donald Trump 0` = "Democrat", `Donald Trump 1` = "Republican"),
                      values = c("#8080ff", "#ff8080", "#0000ff", "#ff0000"),
                      name = "Political Affiliation", guide = "legend") +
  theme_ridges(grid = FALSE)

该代码绘制了这个数字:

在此处输入图像描述

几乎是完美的,但问题是,浅色和深色之间的交替已关闭。前两条线是深色的,然后是两条浅色线。我需要保留标签,但也要保持准确的周期性交替,如第一张图所示。

有任何想法吗?谢谢!

4

2 回答 2

2

啊,我想通了。不要覆盖“ft_newnum”变量,而是创建一个新变量(ft_newnum2)。

data3$ft_newnum2 <- factor(data3$ft_newnum,
                       levels = c(2,3,4,5,6,7,9,11,12, 13, 14, 15),
                       labels = c("Donald Trump", "Christian fundamentalists", "Elites", 'Republicans', 'Denison Republicans', 'Denison Greeks', 'Denison Varsity Athlete','Hillary Clinton', 'Denison Democrats', 'Democrats', 'Bernie Sanders', 'Weinberg'))

ft_num2 用于设置 y 轴,而原始 ft_num 保留并用于填充绘图。

data3 %>%
  mutate(ftFct = fct_rev(as.factor(ft_newnum2))) %>%
  ggplot(aes(y = ftFct)) +
  geom_density_ridges(aes(x = ft, fill = paste(ft_newnum, rep)), 
                      alpha = .8, color = "white", from = 0, to = 100) +
  labs(x = "Feeling Themometer Responses (%)",
       y = " ",
       title = "Republican vs Democratic Views Towards...",
       subtitle = "Analysis unit: students (n = 595)") +
  scale_y_discrete(expand = c(0.01, 0)) +
  scale_x_continuous(expand = c(0.01, 0)) +
  scale_fill_cyclical(breaks = c("2 0", "2 1"),
                      labels = c(`Donald Trump 0` = "Democrat", `Donald Trump 1` = "Republican"),
                      values = c("#8080ff", "#ff8080", "#0000ff", "#ff0000"),
                      name = "Political Affiliation", guide = "legend") +
  theme_ridges(grid = FALSE) +
  theme(legend.position="bottom")

在此处输入图像描述

于 2017-12-18T10:03:31.850 回答
1

基于 Catalan_elections 示例/数据集,显示使用 y 轴中的字符标签交替颜色的可重现示例:

library(data.table)
library(dplyr)
library(forcats)
library(ggridges)
library(ggplot2)

# Making ggridges work with alternating colors and character labels
# in the y-axis.
# The key points are:

# 1. Make sure you create the character-column to use  as a factor, using levels from the numeric values matching the labels to be used in the y-axis (e.g. month.abb)
# 2. For the aes(y=) call use the factor column just created in the prev. step
# 3. And for the fill use the combination of the numeric value and the grouping variable


# Get "Catalan_elections" dataset as a data.table 
dt_Catalan_elections <- as.data.table(Catalan_elections)
# Add a block of data to meet the 12 months and match it with number of unique years.
dt_n <- dt_Catalan_elections[Year==2015,]
dt_n[,Year:=2016]
dt_new <- rbindlist(list(dt_Catalan_elections, dt_n))
old <- as.character(unique(dt_new$Year))
# For each year assign a month (e.g. 1980 - Jan, 1981 - Feb, etc)
dt_new[,month := factor(Year, levels = old, labels = month.abb)]

# get ggridges using month instead of Year for Y-axis labels
p <- dt_new %>%
      ggplot(aes(y = month)) +
      geom_density_ridges(
        aes(x = Percent, fill = paste(Year, Option)), 
        alpha = .8, color = "white", from = 0, to = 100
      ) +
      labs(
        x = "Vote (%)",
        y = "Election",
        title = "Indy vs Unionist vote in Catalan elections",
        subtitle = "Analysis unit: municipalities (n = 949)",
        caption = "Marc Belzunces (@marcbeldata) | Source: Idescat"
      ) +
      scale_y_discrete(expand = c(0.01, 0)) +
      scale_x_continuous(expand = c(0.01, 0)) +
      scale_fill_cyclical(
        breaks = c("1980 Indy", "1980 Unionist"),
        labels = c(`1980 Indy` = "Indy", `1980 Unionist` = "Unionist"),
        values = c("#ff0000", "#0000ff", "#ff8080", "#8080ff"),
        name = "Option", guide = "legend"
      ) +
      theme_ridges(grid = FALSE)
print(p)

##

在此处输入图像描述

于 2019-03-19T23:35:11.347 回答