r - ggplot2 - Factor Specific Styling -


this seems simple question i'm sorry if i'm repeating here. haven't been able find answer far!

i have dataframe looks like:

country <- c("denmark", "denmark", "ireland", "ireland", "uk", "uk") year <- c(2007, 2008, 2007, 2008, 2007, 2008) share <- c(0.5, 0.52, 0.6, 0.72, 0.3, 0.2) oecd <- data.frame(country, year, share) 

(although lot bigger) , have graph made in ggplot2:

q <- ggplot(data=subset(oecd, year >2003 & year < 2013),             aes(year, share, colour = country)) + geom_line() + scale_y_continuous(labels = percent) + ylab("share of total revenue") 

which fine i'd able manipulate graph factor (eg make ireland line bigger, thicker, dashed differently, coloured green etc). have tried this:

alpha <- ggplot(data=subset(oecd, year >2003 & year < 2013), aes(x=year)) +   geom_line(aes(y = share), data = subset(oecd, country == "ireland"), size = 3) +   geom_line(aes(y = share), data = subset(oecd, country != "ireland"), size = 1.5) 

which works enough ireland line not others. in real dataset have 12 factors don't want subset every single country, there better way of doing this?

try this. if want change linetypes, colours , size dependent on country, it's easiest mention when calling ggplot. different linetypes, colours , sizes produced automatically. specific linetypes, sizes etc. can chosen using manual scales.

ggplot(data=subset(oecd, year >2003 & year < 2013),   aes(year, share, colour=country, linetype=country, size=country)) +   geom_line() +   scale_y_continuous(labels=percent) +   scale_size_manual(values=c(2,4,2)) +   scale_linetype_manual(values=c(1,2,1)) +   ylab("share of total revenue") 

here manual scales size , linetype specified. factor country has levels denmark, ireland , uk (in order). vector c(2,4,2) assigns thicker line ireland. in same way, manual scale linetypes assings continuous lines denmark , uk, , dashed line (2) ireland.

hope helps. cheers!


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -