简体   繁体   中英

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. 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).

The data I have is a csv (named planes2), in which is displayed columns for Distance, Paperclips (amount) and Height for each "pilot".

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:

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. You could also include color = w in your aes() call to represent the data without the use of facets.

Edit: Substitute your csv's header names for h, w and X in my code.

Edit: As David pointed out, you need to call the plotting object, pilots.plot , to have R display it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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