R - ggplot line color (using geom_line) doesn't change
Your first code should be
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")
You need to put color
outside 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'))
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 is usually the preferred way.
ggplot geom_line - setting colour of lines doesn't work?
Try this:
ggplot(myData, aes(x = year, y = value, country = country, colour = I("grey"))) +
geom_line()
R ggplot, set colour for one geom_line but not another
Plot mdt
lines with separate geom_line
layers and specify colors in them.
For name A plot mean line using: geom_line(data = mdt[name == "A"], col = "#ff5a32")
with redish color. For thinner lines specify similar color with scale_color_manual
.
library(data.table)
library(ggplot2)
mdt <- dt[, .(value = mean(value)), by = .(name, date)]
ggplot(dt, aes(date, value)) +
geom_line(aes(group = type, col = name)) +
geom_line(data = mdt[name == "A"], col = "#ff5a32", size = 2) +
geom_line(data = mdt[name == "B"], col = "#475cd3", size = 2) +
scale_colour_manual(name = "Name",
values = c("#ff987f", "#8c99e4")) +
labs(x = "Date",
y = "Value",
title = "Change in value over time") +
theme_classic()
How to change color (automatically) in geom_line() using R programming language
Try first creating the plot with ggplot2
functions and be careful that fill
and color
are different things. In order to have a dynamic style you could use ggplotly()
from plotly
package that allows for some interactive values and customization. Here the code:
library(plotly)
#Code
G1 <- ggplot(data=data_1, aes(x=Month, y=rain, color=factor(Year)))+
geom_line(stat="identity")+
theme_minimal()+
#geom_col(width = 0.05, position = position_dodge(0.7))+
xlim(0,12)+
ylim(0,800)+
xlab("Month")+
ylab("Rain")+
ggtitle("Rain according to Month") +
guides(fill = guide_legend(ncol = 2))
#Dynamic
ggplotly(G1)
Output:
Some data used:
#Data
data_1 <- structure(list(tem = c(16.976, 19.9026, 24.3158, 28.1834, 27.8892,
28.8925, 28.3327, 27.9243, 27.6057, 27.0887, 16.976, 19.9026,
24.3158, 28.1834, 27.8892, 28.8925, 28.3327, 27.9243, 27.6057,
27.0887, 16.976, 19.9026, 24.3158, 28.1834, 27.8892, 28.8925,
28.3327, 27.9243, 27.6057, 27.0887), Month = c(1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), Year = c(1901,
1901, 1901, 1901, 1901, 1901, 1901, 1901, 1901, 1901, 1902, 1902,
1902, 1902, 1902, 1902, 1902, 1902, 1902, 1902, 1902, 1902, 1902,
1902, 1902, 1902, 1902, 1902, 1902, 1902), rain = c(18.5356,
16.2548, 70.7981, 66.1616, 267.215, 341.042, 540.907, 493.21,
291.549, 199.171, 18.5356, 16.2548, 70.7981, 66.1616, 267.215,
341.042, 540.907, 493.21, 291.549, 199.171, 118.5356, 116.2548,
170.7981, 166.1616, 367.215, 441.042, 640.907, 593.21, 391.549,
299.171)), row.names = c("1", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "11", "21", "31", "41", "51", "61", "71", "81", "91",
"101", "12", "22", "32", "42", "52", "62", "72", "82", "92",
"102"), class = "data.frame")
Cannot change colors of lines in ggplot
The aes() function is for mapping variables to aesthetic attributes not for changing geoms' properties, you have to specify those outside aes() function, like this:
ggplot(data=SLLN, aes(x=X1, y=X2, group=1)) +
geom_line(aes(colour = "Variable name A")) +
geom_hline(aes(yintercept=6, colour = "Variable name B"), linetype="dashed") +
scale_color_manual(values = c("black","blue")) +
... (the rest of your code)
ggplot2 geom_line colors by group but with different colorcodes
You could use the ggnewscale
package
library(ggnewscale)
ggplot(df, aes(x = variable1)) +
geom_line(aes(y = variable2, color = group1)) +
scale_colour_manual(values = color_group) +
new_scale_color() +
geom_line(aes(y = variable3, color = group1)) +
scale_colour_manual(values = color_flag)
Related Topics
What Does the Error "Arguments Imply Differing Number of Rows: X, Y" Mean
What Are the Differences Between Concatenating Strings with Cat() and Paste()
Convert Hours:Minutes:Seconds to Minutes
Convert Sequence of Longitude and Latitude to Polygon via Sf in R
Spatialpolygons - Creating a Set of Polygons in R from Coordinates
How to Change the Format of an Individual Facet_Wrap Panel
Extracting a Random Sample of Rows in a Data.Frame with a Nested Conditional
Reading Information from a Password Protected Site
Programming-Safe Version of Subset - to Evaluate Its Condition While Called from Another Function
Plotting Ordiellipse Function from Vegan Package Onto Nmds Plot Created in Ggplot2
How to Format Data for Plotly Sunburst Diagram
How to Add a Page Break in Word Document Generated by Rstudio & Markdown
R, Find Duplicated Rows , Regardless of Order
Create 3D Plot Colored According to the Z-Axis
Convert/Export Googleway Output to Data Frame
Add Raster to Ggmap Base Map: Set Alpha (Transparency) and Fill Color to Inset_Raster() in Ggplot2