以此为出发点:ggplot/mapping US counties — problems with visualization shapes in R
我想为fill变量添加一个操作控件。类似的东西:
RECEIVE_BOOT_COMPLETED
但是,当然,ggplot不会将ObesityYear变量识别为数据帧的一部分并给出错误:
eval(expr,envir,enclos)中的错误:object' ObesityYear'找不到
评论之后编辑LED决议
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"))
答案 0 :(得分:0)
为此,您需要使用aes_string
才能在picker
函数中使用字符串选项。
看看这个例子:
#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)))
解决方案:
library(manipulate)
manipulate({
ggplot(dt, aes_string('a', 'b', colour=choose_var )) + geom_point()},
choose_var = picker('fillvar1','fillvar2','fillvar3'))
很难上传图片以显示交互性,但您会在上面的代码中注意到,每次选择不同的值时,点的颜色都会相应地改变。