Reputation: 685
I have a df, df_Filtered
, that looks like this:
Country Region Sales Year Colour
Germany Berlin 2000 2000 #FF0000
Germany Hamburg 1500 2001 #33CC33
Germany Kiel 2150 2002 #00FF00
UK London 1200 2000 #CC0000
UK York 1300 2001 #FFFF33
UK Leeds 2000 2002 #339900
Japan Tokyo 500 2000 #66CC00
Japan Kyoto 750 2001 #990099
I want to plot the data for each region and year wrt the sales value:
ggplot(df_Filtered, aes(x = Year, y = Sales, colour = Region, scale_y_continuous(breaks = 1), size=mysize, labels=as.matrix(df_Filtered_regions))) + geom_line() +
labs(x = "Years", y = "Sales", title = NULL) +
scale_x_continuous(breaks = c(2000, 2001, 2002)) +
scale_size(range = c(1, 4), guide="none") +
theme(panel.background = element_blank()) +
theme(legend.position="bottom") +
scale_color_discrete(name=NULL) +
theme(plot.background = element_rect(colour = "black", size = 1)) +
theme(axis.line = element_line())
Note that I also have a vector for the size of the lines that I want to use in the real data. In the real code the lines representing the average country value is thicker, but to make it simple:
Size = 2
df_Filtered$mysize <- rep(Size, nrow(df_Filtered))
I want to use the colours from the column Colour
in df_Filtered
for the lines in the plot. How do I do this?
(I previously tried to assign the colors a by an automatic process, but that did not work out very well: Different colour palettes for different series ggplot )
Upvotes: 0
Views: 1905
Reputation: 78842
Please make an effort in future questions to provide working code, esp since the ggplot2 construct is seriously incorrect in many, many places. I also strongly suggest you do not use the colors you've chosen for the final plot. Finally, decimal ascii code character 32 (i.e " ") is free and I suspect adopting and adhering to a code format style would help avoid some of the plot construct errors in the future.
You can use pre-generated geom-component colors using I()
which marks the column "asis
" which informs ggplot2 to just use the values specified.
library(ggplot2)
read.csv(text="Country,Region,Sales,Year,Colour
Germany,Berlin,2000,2000,#FF0000
Germany,Hamburg,1500,2001,#33CC33
Germany,Kiel,2150,2002,#00FF00
UK,London,1200,2000,#CC0000
UK,York,1300,2001,#FFFF33
UK,Leeds,2000,2002,#339900
Japan,Tokyo,500,2000,#66CC00
Japan,Kyoto,750,2001,#990099", stringsAsFactors = FALSE) -> xdf
xdf$mysize <- rep(2, nrow(xdf))
ggplot(xdf) +
geom_line(
aes(
x = Year, y = Sales, group = Country,
colour = I(Colour), size = mysize
)
) +
scale_x_continuous(breaks = c(2000, 2001, 2002)) +
scale_y_continuous(breaks = 1) +
scale_size(range = c(1, 4), guide = "none") +
labs(x = "Years", y = "Sales", title = NULL) +
theme(axis.line = element_line()) +
theme(panel.background = element_blank()) +
theme(plot.background = element_rect(colour = "black", size = 1)) +
theme(legend.position = "bottom")
ggplot2 plotting idioms get easier to grok as you use them. Practice is essential.
You want a legend, but you didn't work through how to use a manual color scale. We'll build a named vector from the data you specified:
manual_scale_colors <- setNames(xdf$Colour, xdf$Region)
then remove the I()
and add back the manual scale call:
ggplot(xdf) +
geom_line(
aes(
x = Year, y = Sales, group = Country,
colour = Region, size = mysize
)
) +
scale_x_continuous(breaks = c(2000, 2001, 2002)) +
scale_y_continuous(breaks = 1) +
scale_color_manual(values = manual_scale_colors) +
scale_size(range = c(1, 4), guide = "none") +
labs(x = "Years", y = "Sales", title = NULL) +
theme(axis.line = element_line()) +
theme(panel.background = element_blank()) +
theme(plot.background = element_rect(colour = "black", size = 1)) +
theme(legend.position = "bottom")
Upvotes: 2