2017-12-18 253 views
3

我目前正在使用ggridges庫,以製作'joychart'。我寫了這個:在R(joyplot)中使用ggridges的週期性錯誤

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) 

...這讓我這個數字:

enter image description here

這正是我想要的 - 完美的格式,以及光影色彩之間每行交替,提供一些對比度和增加的可讀性。

接下來,我標記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) 

這碼圖圖中:

enter image description here

幾乎完美,但問題是,光線和暗色之間的交替關閉。前兩行是深色的,後面是兩條淺色的行。我需要保留標籤,但也要保持準確的週期性交替,如第一張圖所示。

任何想法?謝謝!

+1

你能給重複的例子?你更有可能以這種方式得到一個好的答案。 – Axeman

回答

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

enter image description here