【问题标题】:ggplot rcolorbrewer extend and apply to factor dataggplot rcolorbrewer 扩展并应用于因子数据
【发布时间】:2017-05-30 18:43:36
【问题描述】:

紧迫的最后期限意味着我已经没有时间来解决这个问题了,如果之前有人问过,我深表歉意。

我不知道如何(或者如果可能的话)将稍微扩展的 RColorBrewer 调色板应用到 ggplot 线图中的因子数据。

我有以下 3 个变量数据(很多数据点,所以我不会在这里全部粘贴),在一个已经融化的数据框中。

> head(spec.melt)
  Temperature Wavelength CD_Intensity
1          20        260    0.0807910
2          25        260    0.3019320
3          30        260    0.1363325
4          35        260    0.0000000
5          40        260    0.1354045
6          45        260    0.0000000
... # tail(spec.melt)
     Temperature Wavelength CD_Intensity
5635          65        185    0.2499200
5636          70        185    2.5051893
5637          75        185    0.4399785
5638          80        185    4.4368350
5639          85        185    2.0015100
5640          90        185    2.8696540

对于每个温度 (25-90),进行波长扫描 (260nm-190nm),收集 CD 强度,因此有 15 个 260-190nm 测量的光谱折叠在这个 df 中。

到目前为止,我可以很好地绘制这个,并且应用了默认的 ggplot 主题。由于系列数据将是温度,我想使用显示这一点的主题(例如 RColorBrewers "RdYlBu")为它们着色。

我有 15 个温度,默认情况下“RdYlBu”是 11 个色谱。

这是我的代码:

# Import spectrum data
spectrum.obj <- read.table(spectrum,
                           sep = ",",
                           header = TRUE,
                           row.names = 1,
                           check.names = FALSE)

spec.melt <- melt(t(spectrum.obj), value.name = "CD_Intensity", varnames=c("Temperature","Wavelength"))



# # Plot and customise spectrum space
spectrum.img <- ggplot(spec.melt, aes(x=Wavelength, y=CD_Intensity, group=factor(Temperature)))
spectrum.img <- spectrum.img + geom_line(aes(color=factor(Temperature)))
# Axis titles
spectrum.img <- spectrum.img + ylab("CD Intensity (mdeg)") +
                               xlab("Wavelength (nm)") + 
                                guide_legend(title="my awesome title")
# Make transparent
spectrum.img <- spectrum.img + theme_bw()
spectrum.img <- spectrum.img + theme(legend.position = "bottom",
                                     legend.title = element_text("test"),
                                     plot.background = element_blank(),
                                     panel.background = element_blank(),
                                     legend.background = element_blank())



spectrum.img

目前,如果我将 geom_line(aes(color=factor(Temperature))) 更改为其他任何内容,它就会中断。

对于奖励积分,如果有人能告诉我为什么 legend.title 没有产生任何很棒的东西,目前,我被“因素(温度)”所困扰

编辑: 这是原始数据(CSV 格式) https://pastebin.com/ZwtvU8Kq

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我特别建议不要使用 RdYlBu,因为它是一个不同的调色板。这会使您偏向规模的高端和低端。相反,请尝试使用类似 YlOrRd 的感知统一调色板,甚至更好,来自viridis 包的调色板。

    您可以通过保留colour = Temperaturegroup = Temperature factor(Temperature))在代码中使用它们。然后使用scale_color_distiller(palette = "YrOrRd")scale_color_viridis()



    最后,要修复你的标签,使用类似这样的东西要容易得多:

    ggplot(...) + ... + 
      labs(x = "Wavelength (nm)",
           y = "CD Intensity (mdeg)",
           color = "Temperature",
           title = "My title")
    

    【讨论】:

    • 非常感谢。我同意,我认为您选择 viridis 包绝对是明智之举。感谢您的帮助
    【解决方案2】:

    这不能回答您的确切问题,但如果您正在寻找的只是三种颜色之间的渐变,您可以生成手动比例:

    colours <- colorRampPalette(c("blue", "yellow", "red"))(15)
    

    您可以修改颜色的确切版本(深红色与红色)以稍微调整此比例。然后,只需使用通常的:

    scale_color_manual("Awesome title", 
                       labels = as.character(seq(20, 90, length.out = 15)),                  
                       values = colours)
    

    我无法在没有数据的情况下运行您的代码,但是如果您摆脱其他图例标题代码,这应该可以修复您的图例标题。

    更新:

    以下代码适用于您的代码 (ggplot2 2.2.1):

    library(reshape)
    library(ggplot2)
    
    # Import spectrum data
    spectrum.obj <- read.table('test.csv',
                               sep = ",",
                               header = TRUE,
                               row.names = 1,
                               check.names = FALSE)
    
    spec.melt <- melt(t(spectrum.obj), varnames=c("Temperature","Wavelength"))
    
    # # Plot and customise spectrum space
    spectrum.img <- ggplot(spec.melt, aes(x=Wavelength, y=value, group=factor(Temperature)))
    spectrum.img <- spectrum.img + geom_line(aes(color=factor(Temperature)))
    
    # Changing colour
    colours <- colorRampPalette(c("blue", "yellow", "red"))(15) 
    spectrum.img <- spectrum.img + 
                    scale_color_manual("Awesome title", 
                                       labels = as.character(seq(20, 90, length.out = 15)),                  
                                       values = colours)
    
    # Axis titles
    spectrum.img <- spectrum.img + ylab("CD Intensity (mdeg)") +
                                   xlab("Wavelength (nm)")
    
    # Make transparent
    spectrum.img <- spectrum.img + theme_bw()
    spectrum.img <- spectrum.img + theme(legend.position = "bottom",
                                         plot.background = element_blank(),
                                         panel.background = element_blank(),
                                         legend.background = element_blank())
    

    【讨论】:

    • 我已经编辑了带有数据链接的 OP。我想我通过scale_color_manual 尝试了标题,但没有成功,但当时可能还有其他问题。
    • @JoeHealey 我认为问题可能在于我的原始代码中缺少右括号。我的错。我将您的数据保存为“test.csv”,更新后的代码似乎可以满足您的需求。
    • 我同意布赖恩关于正确选择色阶的回答。
    猜你喜欢
    • 2016-03-10
    • 2020-11-06
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 2023-02-22
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多