ggplot2图的子集数据 [英] Subset Data for ggplot2 graph

查看:228
本文介绍了ggplot2图的子集数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ggplot2,并且有一个关于如何对绘图数据进行子集化的问题. 我有以下数据集(示例),需要创建一个折线图,按公司A的年份比较Q1数据.

I am working with ggplot2 and have a question about how to subset data for plots. I have the following dataset (example) and need to create a line plot comparing Q1 data by year of Company A.

x = 2015年第一季度,2016年第一季度,2017年第一季度 y =公司A的数据

x= 2015 Q1, 2016 Q1, 2017 Q1 y= Data for Company A

Company Year    Quarter Data
A       2015    Q1  1
B       2015    Q1  2
C       2015    Q1  3
A       2015    Q2  4
B       2015    Q2  5
C       2015    Q2  6
A       2015    Q3  7
B       2015    Q3  8
C       2015    Q3  9
A       2016    Q1  10
B       2016    Q1  11
C       2016    Q1  12
A       2016    Q2  13
B       2016    Q2  14
C       2016    Q2  15
A       2016    Q3  17
B       2016    Q3  18
C       2016    Q3  19

对于该项目涉及的其他图形,我一直在使用以下代码:

For other graphs involved in this project I've been using this code:

ggplot(df[df$Company=="A",], aes(x=   , y=   , group=1)) +
  geom_line(color='steelblue', size=2) + geom_point(aes(color=Company))+
  xlab("Q1 by Year") +
  ylab("Data") + theme_minimal(base_size=12)+
  ggtitle("  ")+
  theme(plot.title=element_text(hjust=0.5, size=16, face="bold"))+
  theme(axis.text.x=element_text(size=10, vjust=0.5, color="black", face="bold"),
        axis.text.y=element_text(size=10, vjust=0.5, color="black", face="bold"),
        axis.title.x=element_text(size=13, face="bold"),
        axis.title.y=element_text(size=13, face="bold"))+
  theme(aspect.ratio=3/4) + scale_color_brewer(palette="Set2") + 
  theme(legend.position="none")

关于如何为我需要的图将此数据子集化的任何建议?这是我最努力的事情之一.任何帮助,将不胜感激!谢谢!

Any suggestions on how to subset this data for my needed graph? This is one of the things I struggle with the most. Any help would be appreciated! Thank you!

推荐答案

您可以使用dplyr包中的filter子集所需的数据

You can subset the data you want with filter from the dplyr package

library(tidyverse)

df <- read.table(text = "Company Year    Quarter Data
                            A       2015    Q1  1
                            B       2015    Q1  2
                            C       2015    Q1  3
                            A       2015    Q2  4
                            B       2015    Q2  5
                            C       2015    Q2  6
                            A       2015    Q3  7
                            B       2015    Q3  8
                            C       2015    Q3  9
                            A       2016    Q1  10
                            B       2016    Q1  11
                            C       2016    Q1  12
                            A       2016    Q2  13
                            B       2016    Q2  14
                            C       2016    Q2  15
                            A       2016    Q3  17
                            B       2016    Q3  18
                            C       2016    Q3  19",
                 header = TRUE, stringsAsFactors = FALSE)

# subset data
df_select <- df %>% 
  filter(Company == "A" & Quarter == "Q1")
df_select

#>   Company Year Quarter Data
#> 1       A 2015      Q1    1
#> 2       A 2016      Q1   10

ggplot(df_select, aes(x=Year, y=Data, group=1)) +
  geom_line(color='steelblue', size=2) + geom_point(aes(color=Company))+
  xlab("Q1 by Year") +
  ylab("Data") + theme_minimal(base_size=12)+
  ggtitle("  ")+
  theme(plot.title=element_text(hjust=0.5, size=16, face="bold"))+
  theme(axis.text.x=element_text(size=10, vjust=0.5, color="black", face="bold"),
        axis.text.y=element_text(size=10, vjust=0.5, color="black", face="bold"),
        axis.title.x=element_text(size=13, face="bold"),
        axis.title.y=element_text(size=13, face="bold"))+
  theme(aspect.ratio=3/4) + scale_color_brewer(palette="Set2") + 
  theme(legend.position="none")

reprex程序包(v0.2.0)创建于2018-05-22.

Created on 2018-05-22 by the reprex package (v0.2.0).

这篇关于ggplot2图的子集数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆