简体   繁体   English

创建多面图以显示X如何随2个变量变化

[英]Creating faceted plots to show how X varies with 2 variables

this is a pretty basic question, but I'm a pretty basic script writer when it comes to RStudio, so if I could get an outline for the code to use, it would be fantastic. 这是一个非常基本的问题,但是当涉及到RStudio时,我是一个非常基本的脚本编写者,因此,如果我能获得要使用的代码的概述,那将是很棒的。 There are of course resources that give a general idea for faceted plots, or such plots in other applications but I'm having trouble translating this to mine in particular. 当然,有一些资源可以为多面图或在其他应用程序中的此类图提供总体思路,但是我很难将其转换为我的图。

What I'm trying to do is draw a plot that shows how X (in this case distance flown by a paper plane) is affected by 2 variables (in this case height from which the plane is thrown, and the weight of the plane as measured by a number of paperclips). 我要做的是绘制一个图,该图显示X(在这种情况下为纸飞机的飞行距离)如何受2个变量(在这种情况下飞机从其抛出的高度,以及飞机的重量为用许多回形针测量)。

The data I have is a csv (named planes2), in which is displayed columns for Distance, Paperclips (amount) and Height for each "pilot". 我拥有的数据是csv(称为planes2),其中显示了每个“飞行员”的距离,回形针(数量)和高度的列。

I appreciate any inputs! 感谢您的投入! Thanks in advance. 提前致谢。

Sure. 当然。 Here's my implementation: 这是我的实现:

library(ggplot2);                   # Adds the ggplot2 library

# Sample data
pilots <- data.frame(
    w = c(1,2,3,1,2,3,1,2,3),
    h = c(10,10,10,15,15,15,20,20,20),
    X = c(3,2.8,2.6,6,5.8,5.6,9,8.8,8.6));

pilots.plot <- ggplot(pilots) +     # Plot object associated with data frame
    geom_point(aes(h, X)) +         # Points with position based on height and distance
    facet_wrap(~ w);                # Facets for each level of weight

Since you'll be loading the data from a .csv file instead, you'll probably want to replace the second line with something like: 由于您将改为从.csv文件加载数据,因此您可能希望将第二行替换为:

pilots <- read.csv("planes2.csv", header = TRUE);

Check out the options for facet_wrap() to see some other ways you can push the facets around. 请查看facet_wrap()选项,以了解其他一些方面可以推动这些方面。 You could also include color = w in your aes() call to represent the data without the use of facets. 您也可以在aes()调用中包括color = w来表示数据,而无需使用构面。

Edit: Substitute your csv's header names for h, w and X in my code. 编辑:在我的代码中用h,w和X替换csv的标题名称。

Edit: As David pointed out, you need to call the plotting object, pilots.plot , to have R display it. 编辑:正如David指出的那样,您需要调用绘图对象pilots.plot来让R显示它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM