如何解决从R ggplot2的一栏中绘制特定类别
我是R的新手,正在尝试制作一系列剧情。
CITY LOCATION NUMBER_OF_PUBS BEER
Cardiff Wales 100 Brains
Newport Wales 50 Brains
Aberystwyth Wales 400 Brains
Edinburgh Scotland 220 Belhaven
St_Andrews Scotland 20 Belhaven
Aberdeen Scotland 800 Belhaven
Bath England 500 London_Pride
London England 10 London_Pride
Bristol England 200 London_Pride
Birmingham England 100 London_Pride
dublin Ireland 250 Guinness
Cork Ireland 60 Guinness
galway Ireland 750 Guinness
Limerick Ireland 150 Guinness
我正在尝试根据酒吧数量绘制位置的方格和须状图:
但是我只想绘制这些位置的子集。例如,仅从位置列中在威尔士和苏格兰发现的那些。
我找到了此线程:GGPLOT2: how to plot specific selections inthe ggplot() script
并尝试了此操作
t <- as.data.frame(pubs)
ggplot(t,aes(NUMBER_OF_PUBS,LOCATION,fill = factor(BEER))) +
geom_Boxploth(t=subset(t,COHORT="Wales","Scotland"))
但似乎无处可寻...
我确信它非常简单,但似乎无法解决。任何帮助将不胜感激。谢谢。
解决方法
library(tidyverse)
df <- structure(list(CITY = c("Cardiff","Newport","Aberystwyth","Edinburgh","St_Andrews","Aberdeen","Bath","London","Bristol","Birmingham","Dublin","Cork","Galway","Limerick"),LOCATION = c("Wales","Wales","Scotland","England","Ireland","Ireland"),NUMBER_OF_PUBS = c(100L,50L,400L,220L,20L,800L,500L,10L,200L,100L,250L,60L,750L,150L),BEER = c("Brains","Brains","Belhaven","London_Pride","Guinness","Guinness")),class = "data.frame",row.names = c(NA,-14L))
df %>%
#use filter to subset the rows based on the LOCATION column values
filter(LOCATION == c("Wales","Scotland")) %>%
ggplot(aes(NUMBER_OF_PUBS,LOCATION,fill = factor(BEER))) +
geom_boxplot()
由reprex package(v0.3.0)于2020-08-20创建
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。