【问题标题】:How can I change my R code so that there are three lines representing data from different blocks? How can I change my code to edit the legend?如何更改我的 R 代码,以便有三行代表来自不同块的数据?如何更改代码以编辑图例?
【发布时间】:2021-11-20 08:58:30
【问题描述】:

这是我用来制作我的图的代码:

Running_Accuracy_Across_Addition %>%
  ggplot(mapping = aes(x = Trials, y = Accuracy, group = Block, color = Block)) + 
  scale_x_discrete(name = "Trials", limits = c("First Ten", "Second Ten", "Third Ten", "Fourth Ten"), labels = c("First Ten" = "5", "Second Ten" = "15", "Third Ten" = "25", "Fourth Ten" = "35")) +
  scale_y_continuous(name = "Proportion of Correct Addition Responses Over 10 Trials", limits = c(0,1.0)) +
  geom_line() +
  geom_point(alpha = 0.5, size = 0.8) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"), text = element_text(family="Times New Roman", size = 12)) +
  labs(title = "Change in Addition Response Accuracy As The Amount of Available Time Reduces", x = "Trials", y = "Proportion of Correct Addition Responses Over 10 Trials")

这是该图的图像:

【问题讨论】:

  • 首先,您可以将 Block 变量转换为离散变量...此外,添加示例数据将使其他人更容易帮助您(以代码形式添加数据,而不是表格或图片。另见here

标签: r ggplot2 line legend


【解决方案1】:

将您的分组变量更改为如下因素:

df$Block <- factor(df$Block)

示例:

library(ggplot2)

# Load example data
data(iris)

# Dummy trials variable
iris$trials <- 1:nrow(iris)

color= 是数字时

# Plotting behavior for numeric groups
iris$Species_numeric <- as.numeric(iris$Species)
class(iris$Species_numeric)
#> [1] "numeric"

ggplot(data = iris) +
  geom_line(aes(trials, Petal.Length, color = Species_numeric))

color= 是一个因素时

# Plotting behavior with factor groups
iris$Species_factor <- factor(iris$Species_numeric)
class(iris$Species_factor)
#> [1] "factor"

ggplot(data = iris) +
  geom_line(aes(trials, Petal.Length, color = Species_factor))

由 reprex 包 (v2.0.1) 于 2021-09-28 创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 2011-11-03
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多