【问题标题】:Linetype and Guide options for ggplot2 when using geom_smooth with continuous variables将 geom_smooth 与连续变量一起使用时 ggplot2 的线型和引导选项
【发布时间】:2014-04-28 16:42:56
【问题描述】:

我正在使用 ggplot2 和 geom_smooth 函数从 lm 模型制作预测线。我在我的 ggplot 代码中使用连续的 y 和 x 函数。

我正在尝试为图中的每条线创建不同的线型(即变量“位置”,范围为 -1,0 或 1),并且我希望指南中列出的每个位置(其中 - 1 = 位置 A,0 = 位置 B,1 = 位置 C)。我的代码的可复制摘录如下:

#test scores that range from 1 to 7
score <- c(6, 3, 5, 6, 7, 2, 4, 6, 3, 5, 4, 3, 3, 1, 3, 3, 3, 5, 2, 3, 2, 2, 7, 3, 7,     5, 4, 1, 3, 2, 7, 6, 6, 3, 6)
#location of the school
location <- c(1,  0,  0,  0,  1,  1,  1, -1,  1, -1,  0, -1,  0,  1,  0,  0,  0, -1,     0, -1,  0, -1,  0,  0,  0,  0,  0, -1,  0, -1,  0,  0,  0,  1,  0)

IQ <- c(0.7171425604,    0.7056850461,    1.3929736220,    0.0633936624,   -0.6872828336,   -1.3665840767,    1.4368569944, 0.7297599487,   -0.5735047485,   -0.6752912747,   -0.6213572428,   -0.6110533924,   -0.7090921238,    0.0501744806, 1.3916802944,   -0.0055243194,    1.3619753292,    1.4406369365,    0.6529601586,    0.0538097896,    1.3821853866, 1.3870600993,    0.0040551996,    0.6600558495,    1.3550162100,    1.3081187951,   -1.7541949601,    1.3768167017, -0.6232446826,   -1.2793074919,    0.0560708725,   -0.5993356051,   -0.5857733192,   -0.6005459705,   -0.6659873442)

df <- data.frame(score, location, IQ)

p1 <- ggplot(data=df,aes(y=score,x=IQ,shape=factor(location))) + 
geom_smooth(method = "lm", se=F) + scale_y_continuous("Test Score",limits=c(1,7)) +
scale_x_continuous("IQ level", limits=c(-2.45, 1.45)) + 
theme_bw() +
theme(axis.text.x=element_text(size=rel(1.2), color = 'black'),
    axis.title.x=element_text(size=rel(1.3)),
    axis.title.y=element_text(size=rel(1.3)),
    axis.text.y=element_text(size=rel(1.2), color = 'black'),
    panel.grid.minor=element_blank(),
    panel.grid.major.x=element_blank(),
    panel.grid.major.y=element_blank(),
    axis.ticks.y = element_blank(), 
    panel.border = element_blank(),
    plot.title = element_text(size = rel(1.4))) 

print(p1)

每当我尝试添加线型或手动线型时,我都会不断收到关于线型如何无法与连续变量(仅离散)映射的错误。有什么办法可以解决这个问题还是我应该使用不同的功能?也许事先为位置(学校 1、学校 2 和学校 3?)中的值创建标签?或者我应该使用线型的替代品吗?我无法在网上或 Wickham 的书中找到解决方案。谢谢!

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您收到此错误消息是因为 location 是连续变量(数字变量在 ggplot 中被视为连续变量),而对于 linetype=,只能使用离散变量。要将其用于linetype=,请将location 转换为因子。要更改图例中的标签,请使用 scale_linetype() 并提供参数 labels=

    ggplot(data=df,aes(y=score,x=IQ,linetype=factor(location))) + 
      geom_smooth(method = "lm", se=F) + scale_y_continuous("Test Score",limits=c(1,7)) +
      scale_x_continuous("IQ level", limits=c(-2.45, 1.45)) +
      scale_linetype(labels=c("Location A","Location B","Location C"))
    

    【讨论】:

    • 啊,谢谢!你是最好的。数小时盯着代码,我需要做的就是替换一个词。 :)
    • 最后一个问题,有没有办法重新标记指南以将 -1、0 和 1 替换为单词?我可以在情节中手动执行此操作吗?
    • 啊,所以将其转换为因子允许我使用 scale_linetype。很高兴知道,非常感谢 Didzis!
    猜你喜欢
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 2016-05-28
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多