Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

How do I make a Faceted Scatterplot in R?

$
0
0

I'm quite new to R and I've been working at this problem for some time and could use some help. I have a CSV file (called cleaned_doc) which contains the rating and percent of a product sold per country, I would like to compare 2 countries in a faceted scatterplot with 2 facets for all of the product sold in those particular countries of origin.

When I try doing it like this

ggplot(data = cleaned_doc, aes(Percent, Rating)) +
  geom_point() +
  labs( y = "Rating", x = "Percent") + 
  facet_grid( ~ Origin ) 

I get faceted scatter plots for all the countries listed. However I just want to compare 2 (for this example I picked Germany and France) there are more components I want to add later but those are mainly aesthetics so I just want to keep it simple for now. Based on what I've seen on Stackoverflow & other places I tried doing it like this

countries <- gather(cleaned_doc, key="measure", value="value", c(Origin["Germany"], Origin["France"]))

answer2 <- ggplot(data = cleaned_doc, aes(Percent, Rating)) +
  geom_point() +
  labs( y = "Rating", x = "Percent") + 
  facet_grid(~ countries )

However then I get the following error

Error: At least one layer must contain all faceting variables: `countries`.
* Plot is missing `countries`
* Layer 1 is missing `countries`

and I'm really not sure what that means and what I'm doing wrong. so I would really appreciate any help.


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>