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
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.
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, eg box plots, bar plots, violin plots, dot plots, etc.
scale_color_manual()
is used for lines and points.
Here more info .
You're well on your way: You are making three mistakes:
+
twice in a row in your last line of codescale_fill_manual(...)
, but the aesthetic that is being used in the plot is color
. Thus, the correct function would be scale_colour_manual(...)
.colors()
. Check an individual colour using eg "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"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.