Kai7
Kai7

Reputation: 65

Creating a multi-panel plot of a data set grouped by two grouping variables in R

I'm trying to solve the following exercise:

Make a scatter plot of the relationship between the variables 'K1' and 'K2' with "faceting" based on the parameters 'diam' and 'na' (subdivide the canvas by these two variables). Finally, assign different colors to the points depending on the 'thickness' of the ring (don't forget to factor it before). The graph should be similar to this one ("grosor" stands by "thickness"):

enter image description here

Now, the last code I tried with is the following one (the dataset is called "qerat"): ggplot(qerat, aes(K1,K2, fill=factor(grosor))) + geom_point() + facet_wrap(vars(diam,na))

¿Could somebody give me a hand pointing out where the mistake is? ¡Many thanks in advance!

Upvotes: 2

Views: 1070

Answers (1)

Duck
Duck

Reputation: 39595

Maybe you are looking for a facet_grid() approach. Here the code using a data similar to yours:

library(ggplot2)
#Data
data("diamonds")
#Plot
ggplot(diamonds,aes(x=carat,y=price,color=factor(cut)))+
  geom_point()+
  facet_grid(color~clarity)

Output:

enter image description here

In the case of your code, as no data is present, I would suggest next changes:

#Code
ggplot(qerat, aes(K1,K2, color=factor(grosor)))+
  geom_point() +
  facet_grid(diam~na)

Upvotes: 2

Related Questions