1

Using this as a starting point: ggplot/mapping US counties — problems with visualization shapes in R

I wanted to add a manipulate control for the fill variable. Something similar to this:

if (!require("pacman")) install.packages("pacman")
pacman::p_load(ggplot2, XLConnect, rgdal, RColorBrewer, data.table, mapproj, manipulate)

#additional lines
county.data[obesity.data,obesity09:=PCT_OBESE_ADULTS09]
county.data[obesity.data,obesity10:=PCT_OBESE_ADULTS10]
county.data[obesity.data,obesity13:=PCT_OBESE_ADULTS13]

manipulate({ggplot(map.df, aes(x=long, y=lat, group=group, fill=ObesityYear)) +
scale_fill_gradientn("",colours=brewer.pal(9,"YlOrRd"))+
geom_polygon()+coord_map()+
labs(title="2010 Adult Obesity by County, percent",x="",y="")+
theme_bw()}, ObesityYear = picker("obesity09", "obesity10", "obesity13"))

But of course, the ggplot does not recognize the ObesityYear variable as part of the dataframe and gives the error:

Error in eval(expr, envir, enclos) : object 'ObesityYear' not found

EDIT AFTER COMMENTS LED TO RESOLUTION

manipulate({ggplot(map.df, aes_string(x='long', y='lat', group='group', fill=ObesityYear)) + 
    geom_polygon()+coord_map()+ 
    theme_bw()}, ObesityYear = picker('obesity09', 'obesity10', 'obesity13'))
Community
  • 1
  • 1
Pouya Barrach-Yousefi
  • 1,207
  • 1
  • 13
  • 27

1 Answers1

0

For this to work you need to use aes_string in order to be able to use string choices in the picker function.

Look at this example:

#example data.table
set.seed(5)
dt <- data.table(a = runif(10), 
                 b = runif(10), 
                 fillvar1 = factor(sample.int(2,20, rep=T)), 
                 fillvar2 = factor(sample.int(2,20, rep=T)),
                 fillvar3 = factor(sample.int(2,20, rep=T)))

Solution:

library(manipulate)
manipulate({   
    ggplot(dt, aes_string('a', 'b', colour=choose_var )) + geom_point()},
    choose_var = picker('fillvar1','fillvar2','fillvar3'))

It is hard to upload a picture to show the interactivity but you will notice in the above code that every time you select a different value, the colours of the points will change accordingly.

LyzandeR
  • 37,047
  • 12
  • 77
  • 87
  • `fill` would work in the exact same way, but I cannot use it with `geom_point`. – LyzandeR Oct 19 '15 at 12:49
  • This won't work because the fill requires a continuous variable from the dataframe. I tried your suggestion: Replaced aes with aes_string: manipulate({ggplot(map.df, aes_string(x=long, y=lat, group=group, fill=ObesityYear)) + scale_fill_gradientn("",colours=brewer.pal(9,"YlOrRd"))+ geom_polygon()+coord_map()+ labs(title="2010 Adult Obesity by County, percent",x="",y="")+ theme_bw()}, ObesityYear = picker("obesity09", "obesity10", "obesity13")) Which resulted in: Error in lapply(x, f) : object 'long' not found – Pouya Barrach-Yousefi Oct 20 '15 at 11:52
  • My example would work with continuous variables too. You are using the function wrong. The point of using `aes_string` is to use it with strings. Using `x=long` will not work because `long` is not a string. You should use `x='long'` and the same for all the variables in the `aes_string` function... – LyzandeR Oct 20 '15 at 12:48
  • It should be: `manipulate({ggplot(map.df, aes_string(x='long', y='lat', group='group', fill=ObesityYear)) + scale_fill_gradient("",colours=brewer.pal(9,"YlOrRd"))+ geom_polygon()+coord_map()+ labs(title="2010 Adult Obesity by County, percent",x="",y="")+ theme_bw()}, ObesityYear = picker("obesity09", "obesity10", "obesity13"))` – LyzandeR Oct 20 '15 at 12:52
  • Thanks for your help and I'm banging my head trying to get this to work. Now I get: Error in continuous_scale("fill", "gradient", seq_gradient_pal(low, high, : unused argument (colours = c("#FFFFCC", "#FFEDA0", "#FED976", "#FEB24C", "#FD8D3C", "#FC4E2A", "#E31A1C", "#BD0026", "#800026")) – Pouya Barrach-Yousefi Oct 21 '15 at 14:16
  • 1
    This is a completely different part of your code. Try running the `manipulate` part with only the `ggplot` function and the `geom` function. If it works it means that the problem is not in the manipulate but in another part of your code, in which case you should ask a different question. – LyzandeR Oct 21 '15 at 14:25
  • 1
    This works: manipulate({ggplot(map.df, aes_string(x='long', y='lat', group='group', fill=ObesityYear)) + geom_polygon()+coord_map()+ theme_bw()}, ObesityYear = picker('obesity09', 'obesity10', 'obesity13')) – Pouya Barrach-Yousefi Oct 21 '15 at 15:30
  • @PouyaYousefi There was probably a mistake in `continuous_scale` then. I suggest you post a new question with a reproducible example so that we can help you solve your problem. – LyzandeR Oct 21 '15 at 15:41