繁体   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