微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从R ggplot2的一栏中绘制特定类别

如何解决从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

我正在尝试根据酒吧数量绘制位置的方格和须状图:

Box and whisker plot of pubs

但是我只想绘制这些位置的子集。例如,仅从位置列中在威尔士和苏格兰发现的那些。

我找到了此线程: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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?