I have a dataset called temperature4countries
Year | Spain | Cyprus | Iceland | Austria |
---|---|---|---|---|
1998 | 15 | 17 | 7 | 8 |
1999 | 20 | 21 | 7.5 | 8.5 |
2000 | 16 | 18 | 7 | 8 |
2001 | 17 | 20 | 8 | 8 |
2002 | 17.5 | 19 | 8 | 8 |
2003 | 20 | 21 | 7.5 | 8.5 |
2004 | 20 | 22 | 8 | 9 |
2005 | 20 | 21 | 8.5 | 9.5 |
2006 | 21 | 27 | 9 | 10 |
2007 | 22 | 23 | 9.5 | 10.5 |
2008 | 25 | 24 | 9 | 11 |
temperature <- data.frame(
stringsAsFactors = FALSE,
Year= c(1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008),
Spain = c(15,20,16,17,17.5,20,20,20,21,22,25),
Cyprus = c(17,21,18,20,19,21,22,21,27,23,24),
Iceland = c(7,7.5,7,8,8,7.5,8,8.5,9,9.5,9),
Austria = c(8,8.5,8,8,8,8.5,9,9.5,10,10.5,11),
check.names = FALSE
)
dput(temperature)
My goal is to create a line graph
I used that code
df <- pivot_longer(temperature,
cols = c(Spain, Cyprus, Iceland, Austria),
values_to = "Temperature",
names_to = "Countries")
ggplot(df,
aes(
x = Year,
y = Temperature,
color = Countries
))
geom_line()
ggtitle("Trend of Temperature in 4 European countries")
xlab("Year")
ylab("Temperature[C]")
Again the colors are really ugly. I would like to change it. I used that code
df <- pivot_longer(temperature,
cols = c(Spain, Cyprus, Iceland, Austria),
values_to = "Temperature",
names_to = "Countries")
ggplot(df,
aes(
x = Year,
y = Temperature,
color = Countries
))
geom_line()
ggtitle("Trend of Temperature in 4 European countries")
xlab("Year")
ylab("Temperature[C]") scale_fill_manual(values=c("aqua marine", "teal","lime","yellow"))
But the colors didn't change???
Because I thought that this is the way how to change colors
CodePudding user response:
You should use scale_color_manual
in this case, instead of scale_fill_manual
. Be explicit in your values
param, passing a named vector:
scale_color_manual(values=c("Austria" = "red", "Cyprus" = "blue",
"Iceland" = "green","Spain" = "orange"))
Check colors()
for named colors available. You could use "#008080"
in place of "teal"
and "limegreen"
in place of "lime"
, if you like.
CodePudding user response:
The reason why the color doesn't change is because you cannot use scale_fill_manual
. Change it to scale_color_manual
and you will see the change.
ggplot(df,
aes(
x = Year,
y = Temperature,
color = Countries
))
geom_line()
ggtitle("Trend of Temperature in 4 European countries")
xlab("Year")
ylab("Temperature[C]") scale_color_manual(values=c("aqua marine", "red","blue","yellow"))
Note: that I had to change some colors because it couldn't find "lime" and "teal".
PS. The difference between them is that:
scale_fill_manual()
is used for plots where you have things to fill, e.g. box plots, bar plots, violin plots, dot plots, etc.scale_color_manual()
is used for lines and points.
Here more info.
CodePudding user response:
You're well on your way! You are making three mistakes:
- You added
- You are using
scale_fill_manual(...)
, but the aesthetic that is being used in the plot iscolor
. Thus, the correct function would bescale_colour_manual(...)
. - The colours that you specified are not R colours. You can get an overview of all named colours in R using
colors()
. Check an individual colour using e.g."aquamarine" %in% colors()
Thus, the correct code would be:
ggplot(df,
aes(
x = Year,
y = Temperature,
color = Countries
))
geom_line()
ggtitle("Trend of Temperature in 4 European countries")
xlab("Year")
ylab("Temperature[C]") scale_colour_manual(values=c("aquamarine", "cyan4","limegreen","yellow"))