我有一个R Shiny应用程序,它使用Leaflet创建交互式地图.在此地图上,分类变量用于指定不同类型的点,并使用自定义标记(不同的图标,取决于因子级别)进行可视化.
我想要做的是在图中添加一个图例,但让图例显示各种标记图标而不是纯色.该传说教程不包括这一点.
我遇到了另一个似乎可以解决这个问题的SO答案 - 但它是用JavaScript完成的,我不知道如何翻译它/如果它可以被翻译成在R中工作.任何人都知道如何实现这一目标?
一个基本可重复的例子:
library(leaflet)
# Sample Data
data(quakes)
quakes <- quakes[1:10,]
# Choose Icon:
leafIcons <- icons(
iconUrl = ifelse(quakes$mag < 4.6,
"http://leafletjs.com/docs/images/leaf-green.png",
"http://leafletjs.com/docs/images/leaf-red.png"
),
iconWidth = 38, iconHeight = 95,
iconAnchorX = 22, iconAnchorY = 94)
# Produce Map:
leaflet(data = quakes) %>% addTiles() %>%
addMarkers(~long, ~lat, icon = leafIcons)
Run Code Online (Sandbox Code Playgroud) 我目前正准备在RStudio中进行演示(使用RMarkdown和Knitr,输出到Beamer演示文稿),其中有很多参考文献.
我使用的是非常典型的YAML标头:
---
title: "Title"
author: "Me"
date: "February 27th, 2016"
output:
beamer_presentation
csl: ../../apa.csl
bibliography: ../../RefenceDesk.bib
---
Run Code Online (Sandbox Code Playgroud)
此演示文稿编译并且引用按原样显示,但不幸的是它们都出现在一张幻灯片上(实际上是在页面上运行).有没有办法让参考文献出现在多张幻灯片上?
我正在使用lme4附带的数据集,并且我正在尝试学习如何应用reshape2将其从long转换为宽[在帖子末尾的完整代码].
library(lme4)
data("VerbAgg") # load the dataset
Run Code Online (Sandbox Code Playgroud)
数据集有9个变量; "愤怒","性别"和"身份"不随"项目"而变化,而"resp","btype","原地","模式"和"r2"都有.
我已经成功地使用reshape()将数据集从长格式转换为宽格式:
wide <- reshape(VerbAgg, timevar=c("item"),
idvar=c("id", 'Gender', 'Anger'), dir="wide")
Run Code Online (Sandbox Code Playgroud)
这对123个变量产生316个观测值,并且似乎被正确转换.但是,我没有成功使用reshape/reshape2来重现宽数据帧.
wide2 <- recast(VerbAgg, id + Gender + Anger ~ item + variable)
Using Gender, item, resp, id, btype, situ, mode, r2 as id variables
Error: Casting formula contains variables not found in molten data: Anger
Run Code Online (Sandbox Code Playgroud)
关于重铸如何定义id变量我可能不是100%清楚,但我很困惑为什么它没有看到"愤怒".同样的,
wide3 <- recast(VerbAgg, id + Gender + Anger ~ item + variable,
id.var = c("id", "Gender", "Anger"))
Error: Casting formula contains variables not found in …Run Code Online (Sandbox Code Playgroud) 我被要求使用来重新创建饼图ggplot2,并且很难在绘图中添加第二个标题。我需要在图的左下方和右下方添加标题。
我当前的方法可以通过使用hjust标题放置选项来获得其中一种(0表示左对齐; 1表示右对齐):
library(ggplot2)
dat <- data.frame(variable = c("V1", "V2", "V3"),
value = c(.80,.50,.63))
p1 <- ggplot(dat,
aes(x = 1, y = value, fill = variable)) +
geom_bar(stat = "identity") +
coord_polar(theta = "y") +
theme(legend.position = 'none',
plot.caption = element_text(hjust = 1)) +
labs(caption = "RIGHT CAPTION")
print(p1)
Run Code Online (Sandbox Code Playgroud)
这将产生:
我已经看到了一些可以使用的方法,annotate()但似乎无法使它们一起使用coord_polar()。
有谁知道我如何获得第二个字幕显示在图的左侧(与右侧字幕水平对齐)?也许可以覆盖仅具有左标题的空白层?