我有一个 ggplot 我想变成交互式的。但是,我不希望所有对象都具有交互性,因为有些对象实际上是用作一种背景图(在这种情况下,是一排排温室植物)。顶部方块中的数据应该是交互式的,而不是绿色的植物点或灰色的行。
这甚至可以通过情节来实现吗?如果是这样:如何?
我的代码和结果(我添加了theme_update()
因此复制看起来与我的完全一样,尽管它不太重要):
代码
# counts in greenhouse
Wtot <- c(10,65,139,87)
plant <- c(15,15,30,30)
row <- c(10,20,10,20)
df <- data.frame(Wtot,plant,row)
# df for greenhouse background
nrows = 40
nplants = 50
greenh <- data.frame(
Row <- rep(1:nrows, each=nplants),
Plant <- rep(1:nplants, times=nrows),
Plantnr <- paste("plant",1:(nrows*nplants))
)
# plottheme
theme_set(theme_bw())
theme_update(axis.title.x = element_text(size = 16, colour = 'black'),
axis.title.y = element_text(size = 16, colour = 'black', angle = 90),
axis.text.x = element_text(size = 12, colour = 'black'),
axis.text.y = element_text(size = 12, colour = 'black'),
legend.text = element_text(size = 14, colour = 'black'),
legend.title = element_blank(),
legend.position = "right",
legend.box = "vertical",
strip.text.x = element_text(size = 14, colour = 'black'),
strip.text.y = element_text(size = 14, colour = 'black', angle = -90),
panel.background = element_rect(fill = "gray94"),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank())
# plot
p <- ggplot(data=df, aes(row, plant, colour=Wtot)) +
xlim(0,nrows) +
ylim(0,nplants) +
coord_equal() +
geom_vline(xintercept=1:nrows, colour="darkgrey", alpha=0.5, size=0.7) +
geom_point(data=greenh, aes(x=Row, y=Plant), colour="darkgreen", alpha=0.3) +
geom_point(aes(fill=Wtot, size=Wtot), colour="black", pch=22) +
scale_fill_gradient2(low="green", mid="yellow", high="red", na.value="grey",
limits=c(0,300), midpoint=150, breaks=c(0,75,150,225,300)) +
scale_size_continuous(range=c(3,6), limits=c(0,300), breaks=c(0,75,150,225,300)) +
guides(fill=guide_legend(), size = guide_legend())