R-ggplot线颜色(使用geom_line)不变 [英] R - ggplot line color (using geom_line) doesn't change

查看:1211
本文介绍了R-ggplot线颜色(使用geom_line)不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ggplot(geom_line)在一个图上绘制2条线时,该线的颜色与我设置的颜色不符。我想要黑色和蓝色线条,但结果是红色和蓝色。我在没有(第一个代码)和(第二个)'scale_color_manual'的情况下进行了尝试,还尝试了用颜色插入颜色,结果相同:

When ploting 2 lines on one plot with ggplot(geom_line) the color of the line doesn't compre to the color that I set. I want the lines black and blue, but the outcome is red and blue. I tried it without (first code) and with (second) 'scale_color_manual', also tried with colour insted of color, with the same result:

fisrt代码:

fisrt code:

  ggplot(data=main_data) +
  # black plot
  geom_line(aes(x=vectors_growth_rate_with_predator, 
                y=disease_prevalnce_with_predator,
                color = "black")) + 
  # blue plot
  geom_line(aes(x=vectors_growth_rate_with_predator, 
                y=disease_prevalnce_without_predator,
                color = "blue"))

第二个代码:

PrevVSGrowth = 
  ggplot(data=main_data) +
  # black plot
  geom_line(aes(x=vectors_growth_rate_with_predator, 
                y=disease_prevalnce_with_predator)) + 
  # blue plot
  geom_line(aes(x=vectors_growth_rate_with_predator, 
                y=disease_prevalnce_without_predator))

PrevVSGrowth + scale_color_manual(values=c(disease_prevalnce_with_predator= 'black',
                                           disease_prevalnce_without_predator = 'blue'))

推荐答案

您的第一个代码应为

ggplot(data=main_data) +
# black plot
geom_line(aes(x=vectors_growth_rate_with_predator, 
              y=disease_prevalnce_with_predator),
          color = "black") + 
# blue plot
geom_line(aes(x=vectors_growth_rate_with_predator, 
              y=disease_prevalnce_without_predator),
          color = "blue")

您需要将 color 放在<$之外c $ c> aes()。

第二个代码需要将数据从宽格式转换为长格式。您可以通过多种方式来执行此操作,以下方法将为您工作。

For your second code you need to reshape your data from wide to long format. You can do this in many ways, the following should work for you.

library(tidyverse)
main_data <- main_data %>% 
               gather(key, value, c("disease_prevalnce_with_predator",
                                    "disease_prevalnce_without_predator")
PrevVSGrowth <- ggplot(data=main_data) +
 geom_line(aes(x=vectors_growth_rate_with_predator, 
               y=value,
               col = key))

PrevVSGrowth + 
scale_color_manual(values = c(disease_prevalnce_with_predator= 'black',
                              disease_prevalnce_without_predator = 'blue'))






在第一个图中,在每次调用 geom_line()时,我们将美学设置为为固定值,这将创建两个仅包含值的新变量在OP的示例中,值 black和 blue随后分别缩放为红色和浅蓝色,


In the first plot we set an aesthetic to a fixed value, in each call to geom_line(). This creates two new variables containing only the value "black" and "blue", respectively. In OP's example the values "black" and "blue" are then scaled to red and lightblue and a legend is added.

在第二个图中,我们映射将颜色美感映射到变量()。这通常是首选方式。

In the second plot we map the colour aesthetic to a variable (key in this example). This usually the preferred way.

这篇关于R-ggplot线颜色(使用geom_line)不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆