简体   繁体   中英

ggplot2 - 4 categorical and 1 continuous variable

I am trying to create a graph like the one below in R but it has proved to be harder than just doing it in excel. X is a categorical variable, y is continuous, then colour would be a dichotomous variable and line type another. How can I go about doing it?I tried using the following code but if I change one of the line dimensions or colour it just gives a notice "Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line". How can I adapt it?

ggplot(RT, aes(x= Block, y= mean, group = Probability, color=Probability))+ 
  geom_errorbar(aes(ymin=mean-CI, ymax=mean+CI), width=.1, 
            position=position_dodge(0.05)) + xlab("Epoch") +
 geom_line(aes(linetype=Probability), size = 1) + 
 geom_point(aes(shape=Probability))+
facet_wrap(~ Session, scales="free") + coord_cartesian(ylim = c(370, 530)) + 
  scale_color_grey(start=0.85, end=0.2)+
  theme_classic() 

5

在此处输入图像描述

It's possible to do by adding a grouping variable and group aesthetic. Here's a full reprex:

library(ggplot2)

vals <- c(11, 13, 12, 14)

df <- data.frame(
  x = rep(1:4, 4),
  y = c(vals, 2 * vals, 3 * vals, 4 * vals),
  colours = rep(c("#bf6424", "black"), each = 8),
  linetype = rep(rep(2:1, each = 4), 2),
  group = rep(1:4, each = 4))


ggplot(df, aes(x, y, colour = colours, linetype = linetype, group = group)) +
  geom_line(size = 2) +
  scale_color_identity() +
  scale_linetype_identity() +
  theme_classic()

Created on 2020-08-03 by the reprex package (v0.3.0)

It would be easier to see what is going wrong if you post a sample of your data so that we could take your ggplot code and run it to see what happens.

However, a quick inspection of your code reveals some issues. You are mapping your Probability variable to four aesthetics: group, color, linetype, and shape. Each of group, color, and linetype needs to be mapped to a different variable.

Here is a minimal example. In this example, I have a dummy variable called group that separates each line. I map var1 to color and var2 to linetype. Since my data only has one point per combination of x, y, group, color, and linetype, I need to set stat = identity to let geom_line know this is intentional.

df <- data.frame(x = factor(rep(1:4, 4)),
                 y = c(10, 15, 10, 15, 20, 25, 20, 25, 30, 35, 30, 35, 40, 45, 40, 45),
                 group = factor(c(rep(0, 4), rep(1, 4), rep(2, 4), rep(3, 4))),
                 var1 = factor(c(rep(0, 8), rep(1, 8))),
                 var2 = factor(c(rep(0, 4), rep(1, 4), rep(0, 4), rep(1, 4)))
                 )
ggplot(df) + 
  geom_line(aes(x, y, group = group, color = var1, linetype = var2), stat="identity")

Here is the plot it produces.

具有四条不同颜色和线型的线的线图

The trick is to make an interaction variable from your vars var1 and var2 which gives a factor with four levels representing the four series . (Basically this corresponds to the group variable in the answer by @AllanCameron). This new interaction variable can then be mapped on both color and linetype which results in one legend. The colors and linetypes can then be set via scale_color_manual and scale_linetype_manual . try this:

library(ggplot2)
library(dplyr)

set.seed(42)

df <- data.frame(
  var1 = factor(rep(1:2, each = 20, length = 40)),
  x = rep(1:5, each = 4),
  var2 = factor(rep(1:2, each = 2)),
  var3 = factor(rep(1:2, each = 1)),
  y = runif(40, 300, 500)
)

df %>% 
  mutate(series = interaction(var1, var2, sep = "_")) %>% 
  ggplot() + 
  geom_line(aes(x, y, color = series, linetype = series), stat="identity") + 
  scale_color_manual(values = c("orange", "orange", "black", "black"),
                     labels = paste0("series", 1:4)) +
  scale_linetype_manual(values = c("dashed", "solid", "dashed", "solid"),
                        labels = paste0("series", 1:4)) +
  facet_wrap(~ var3, scales="free") +
  theme(legend.position = "bottom")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM