r - ggplotly 不提供交互式人口金字塔
问题描述
使用 ggplot,我可以在 Shiny 框架内创建一个完美运行的人口金字塔。我尝试使用 ggplotly 让它更漂亮,但无法获得正确的结果。这是成功的代码。
pyramidPlot <- reactive ({
ggplot (pyramid(), aes(x = Age_Group, y = Percentage, fill = Sex,
label=Percentage)) +
geom_bar(subset = .(Sex == "Females"), stat = "identity") +
geom_bar(subset = .(Sex == "Males"), stat = "identity") +
coord_flip() +
labs(
x = "Age Groups",
y = "Population Size (%)",
caption = (""),
face = "bold.italic"
) +
scale_fill_brewer(palette = "Set1") +
theme_bw() +
scale_y_continuous(labels=abs_comma <- function (x, ...) {
format(abs(x), ..., big.mark = ",", scientific = FALSE, trim =
TRUE)
})+
theme(plot.title = element_text(
hjust = 0.5,
size = 15,
colour = "Black",
face = "bold.italic"
),axis.text=(blue.bold.italic.10.text),
axis.title=blue.bold.italic.14.text)+
ggtitle(paste(
"Population Pyramid for",
input$county,
"in",
input$years
))
})
output$pyramid <- renderPlot ({
pyramidPlot()
})
这是失败的代码。我稍微改变了上面的代码,试图让ggplotly参与进来。我收到“'bar'对象没有这些属性:'mode'”的错误。我希望有一个人口金字塔,它可以交互地显示条形上的百分比和相应的性别。
pyramidPlot <- reactive ({
g<- ggplot(pyramid(), aes(x = Age_Group, y = Percentage, fill = Sex,
label=Percentage)) +
geom_bar(subset = .(Sex == "Females"), stat = "identity") +
geom_bar(subset = .(Sex == "Males"), stat = "identity") +
coord_flip() +
labs(
x = "Age Groups",
y = "Population Size (%)",
caption = (""),
face = "bold.italic"
) +
# geom_text(aes(y = Percentage + 1 * sign(Percentage), label =
Percentage2),
# position = position_dodge(width = 0), hjust=0,vjust=0
,size =5, colour = "Black",
# face = "bold.italic"
# )+
scale_fill_brewer(palette = "Set1") +
theme_bw() +
scale_y_continuous(labels=abs_comma <- function (x, ...) {
format(abs(x), ..., big.mark = ",", scientific = FALSE, trim =
TRUE)
})+
# annotate("text", x = 4.3, y = -100, label = "Women", fontfacet =
"bold") +
# annotate("text", x = 4.3, y = -200, label = "Men", fontfacet =
"bold") +
theme(plot.title = element_text(
hjust = 0.5,
size = 15,
colour = "Black",
face = "bold.italic"
),axis.text=(blue.bold.italic.10.text),
axis.title=blue.bold.italic.14.text)+
ggtitle(paste(
"Population Pyramid for",
input$county,
"in",
input$years
))
ggplotly(g)
})
output$pyramid <- renderPlot ({
pyramidPlot()
})
解决方案
推荐阅读
- here-api - 地理编码自动完成示例不会使用我的 api 密钥返回结果
- javascript - 通过js将多个元素传递给php
- java - 如何避免 URIBuilder 将逗号编码为“%2C”或查询参数中的其他字符
- excel - 在 .PDF 中导出范围时插入分页符
- php - PHP 有条件地需要/使用库类
- java - 如何正确映射不明确的处理程序方法?
- ios - 如何在发布前查看 iOS App Store URL?
- point-cloud-library - 如何使用 pcl 库在区域增长后获得特定的集群?
- arangodb - 有没有办法在 ArangoDb 中“触摸”文档
- statistics - DAX - 复杂滚动平均线