[英]Making multiple facets in ggplot2
Hello I the following data set called States
from the package carData
in R. I am trying to plot the following data. 你好,我叫下面的数据集
States
从包carData
在R.我想绘制以下数据。
head(States)
region pop SATV SATM percent dollars pay
AL ESC 4041 470 514 8 3.648 27
AK PAC 550 438 476 42 7.887 43
AZ MTN 3665 445 497 25 4.231 30
AR WSC 2351 470 511 6 3.334 23
CA PAC 29760 419 484 45 4.826 39
CO MTN 3294 456 513 28 4.809 31
What I want to do is create a plot which shows four data from this chart. 我想做的是创建一个图表,显示该图表中的四个数据。 I want like a multiple facet chart.
我想要一个多方面的图表。
Where my two variable on the vertical axes are SATV and SATM and my two variables on the horizontal axis are dollar and pay. 我在垂直轴上的两个变量是SATV和SATM,在我水平轴上的两个变量是美元和工资。
So for example there would be like four boxes and my top right would be the relation between pay and satv. 例如,将有四个方框,而我的右上角将是薪水和卫星之间的关系。
How can I do this using lattice or ggplot2. 我该如何使用格子或ggplot2。
I assumed at first glance that this was a matter of making a scatterplot matrix. 乍一看,我认为这是制作散点图矩阵的问题。 @Punintended suggested
GGally::ggpairs
, and there might be something in GGally::ggduo
that works too. @Punintended建议使用
GGally::ggpairs
,并且在GGally::ggduo
中可能也可以使用。 But since you aren't looking for all combinations of variables, I realized that this is actually about reshaping the data: You have 2 independent variables, dollars
and pay
, and 2 dependents, SATV
and SATM
. 但是由于您并没有在寻找变量的所有组合,所以我意识到这实际上是在重塑数据:您有2个独立变量(
dollars
和pay
)和2个因变量( SATV
和SATM
。 You can reshape it so there's a column of test types and a column of test scores, and then reshape again to make a column of funding types and a column of funding amounts. 您可以对其进行重塑,以便有一列测试类型和一列测试分数,然后再次重塑以创建一列资金类型和一列资金金额。 I did this with two calls of
tidyr::gather
. 我通过两次
tidyr::gather
做到这一点。
library(dplyr)
library(tidyr)
library(ggplot2)
data(States, package = "carData")
states_long <- States %>%
gather(key = test_type, value = score, SATV, SATM) %>%
gather(key = funding_type, value = funding_value, dollars, pay)
head(states_long)
#> region pop percent test_type score funding_type funding_value
#> 1 ESC 4041 8 SATV 470 dollars 3.648
#> 2 PAC 550 42 SATV 438 dollars 7.887
#> 3 MTN 3665 25 SATV 445 dollars 4.231
#> 4 WSC 2351 6 SATV 470 dollars 3.334
#> 5 PAC 29760 45 SATV 419 dollars 4.826
#> 6 MTN 3294 28 SATV 456 dollars 4.809
After that, facetting will work, because you now have some way to split the data apart, by funding type (dollars spent or pay) versus test type (verbal or math). 之后,方面将起作用,因为现在您可以通过某种方式将数据按资金类型(花费或支付的美元)与测试类型(语言或数学)分开。
ggplot(states_long, aes(x = funding_value, y = score)) +
geom_point() +
facet_grid(test_type ~ funding_type, scales = "free")
Created on 2018-07-11 by the reprex package (v0.2.0). 由reprex软件包 (v0.2.0)于2018-07-11创建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.