Using xkcd package with ggplot2: How do I color a bar graph but keep the man solid?
colors, ggplot2, r
Solution
This works:
ggplot() +
xkcdrect(
aes(xmin = A, xmax = A + .25, ymin = 0, ymax = C, fill = B),
Ride
) + xkcdman(mapping,dataman2) + coord_flip() +
scale_fill_continuous(low=c("#990000"), high=c("#009900"))
Just add the `fill` aesthetic to `aes`.
Problem
I am using the pre-eminent package for noteworthy data analysis, xkcd, but I am having difficulty with the colors. I am trying to add the xkcd man standing next to a colorful `geom_bar`, but I can only do colorful `geom_bar` or man next to bar graph, not both. ``` library(xkcd) library(ggplot2) set.seed(16) #borrowed from vignette library(extrafont) download.file("http://simonsoftware.se/other/xkcd.ttf", dest = "xkcd.ttf") system("mkdir ~/.fonts") system("cp xkcd.tff -t ~/.fonts") library(extrafont) font_import() loadfonts() #creates data from xkcd man: from vignette mapping <- aes( x = x, y = y, scale = scale, ratioxy = ratioxy, angleofspine = angleofspine, anglerighthumerus = anglerighthumerus, anglelefthumerus = anglelefthumerus, anglerightradius = anglerightradius, angleleftradius = angleleftradius, anglerightleg = anglerightleg, angleleftleg = angleleftleg, angleofneck = angleofneck, color = color) dataman2 <- data.frame( x= 3.5, y = 3, scale = 2, ratioxy = 0.5, angleofspine = -pi/2, anglerighthumerus = -pi/6, anglelefthumerus = 7*pi/6, anglerightradius = 4*pi / 3, angleleftradius = 5*pi / 3, angleleftleg = 4*pi / 3, anglerightleg = 5*pi / 3, angleofneck = (3*pi/2) - (pi/10), color=c("red")) #this rotates the xkcd man to match coord_flip dataman2[ ,1:ncol(dataman2) %in% 5:(ncol(dataman2) - 1)] <- dataman2[ ,c(5:(ncol(dataman2) -1))] - pi/2 #Create my df Ride <- data.frame( A = 1:6, B = .01* seq(from = 5, to = 30, by= 5), C = round(runif(6, min = .9, max = 1.1), 2) * c(2,1,2,2,1,2)) #These are the colors I want, but I want to add xkcd man on the right ggplot(Ride, aes(A, C, fill = B)) + geom_bar(stat="identity", width=.1) + coord_flip() + scale_fill_continuous(low=c("#990000"), high=c("#009900")) #+ ``` These are the color bars I'd like. ``` #idea 1 - won't work b/c of fill = B ggplot(Ride, aes(A, C, fill = B)) + geom_bar(stat="identity", width=.1) + xkcdman(mapping,dataman2) #idea 2 - works but b&w; I am trying to add scale_fill_continuous (in comments) ggplot() + xkcdrect(aes(xmin = A, xmax = A + .25, ymin = 0, ymax = C), Ride) + xkcdman(mapping,dataman2) + coord_flip() #+ #scale_fill_continuous(low=c("#990000"), high=c("#009900")) ``` Unfortunately these aren't as colorful. How do I color a bar graph (continuous) but keep the man solid? Thanks for the assistance.