簡體   English   中英

ggplot, plot 兩種線型和 colors

[英]ggplot, plot two linetypes and colors

出於某種原因,我無法弄清楚如何 plot 一個具有兩種不同線型的圖形。 我不想使用方面。 我想在這里將顏色映射到黨派關系,並將線型映射到框架。

Liberals<-c(0.6, 0.75, 0.8, 0.6, 0.75, 0.9)
Conservatives<-c(0.4, 0.25, 0.2, 0.4, 0.25, 0.1)
 partisanship<-rep(c('low', 'medium', 'strong'), 2)

 frame<-c(rep('neutral', 3), rep('partisan', 3))
 frame
 partisanship
 Liberals
 Conservatives
df<-data.frame(Liberals, Conservatives, partisanship, frame) 
df
library(tidyverse)


## This is the data I'm working with

structure(list(partisanship = c("strong", "strong", "medium", 
"medium", "low", "low", "low", "low", "medium", "medium", "strong", 
"strong"), frame = c("partisan", "neutral", "neutral", "partisan", 
"neutral", "partisan", "neutral", "partisan", "neutral", "partisan", 
"neutral", "partisan"), name = c("Conservatives", "Conservatives", 
"Conservatives", "Conservatives", "Conservatives", "Conservatives", 
"Liberals", "Liberals", "Liberals", "Liberals", "Liberals", "Liberals"
), value = c(0.1, 0.2, 0.25, 0.25, 0.4, 0.4, 0.6, 0.6, 0.75, 
0.75, 0.8, 0.9)), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"))->df2


df2%>% 
    ggplot(., aes(x=partisanship, y=value, group=frame, col=name))+geom_line(aes(linetype=frame, col=frame))

這可以通過使用例如interactionframename映射到group審美上來實現。 此外,當您想按name着色時,請從 geom_line 中刪除col=frame

library(tidyverse)

df_long <- df %>% 
  pivot_longer(-c(partisanship:frame))  %>% 
  arrange(., value, frame) 

ggplot(df_long, aes(x=partisanship, y=value, group=interaction(frame, name), col=name)) + 
  geom_line(aes(linetype=frame))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM