2

I am missing some basics in R.

How do I make a plot for each column in a data frame?

I have tried making plots for each column separately. I was wondering if there was a easier way?

library(dplyr)
library(ggplot2)
data(economics)

#scatter plots
ggplot(economics,aes(x=pop,y=pce))+
  geom_point()
ggplot(economics,aes(x=pop,y=psavert))+
  geom_point()
ggplot(economics,aes(x=pop,y=uempmed))+
  geom_point()
ggplot(economics,aes(x=pop,y=unemploy))+
  geom_point()

#boxplots
ggplot(economics,aes(y=pce))+
  geom_boxplot()
ggplot(economics,aes(y=pop))+
  geom_boxplot()
ggplot(economics,aes(y=psavert))+
  geom_boxplot()
ggplot(economics,aes(y=uempmed))+
  geom_boxplot()
ggplot(economics,aes(y=unemploy))+
  geom_boxplot()

All I'm looking for is having 1 box plot 2*2 and 1 2*2 scatter plot with ggplot2. I understand there is facet grid which I have failed to understand how to implement.(I believe this can be achieved easily with par(mfrow()) and base R plots. I saw somewhere else using using widening the data? which i didn't understand.

Zo Wakaki
  • 23
  • 4
  • 1
    Did you try the answers to your [previous question](https://stackoverflow.com/questions/58815296/ggplot-matrix-with-x-y-and-color-category-using-loops/58816998#58816998)? – MSR Nov 12 '19 at 12:17
  • 1
    Thanks for pointing it out @MSR. Hi Zo, the solutions provided in your previous question should solve this.. What problems did you encounter? – StupidWolf Nov 12 '19 at 12:27
  • I couldn't get it working as couldn't understand the solution very well. I am just starting out and I have problem understanding the pivot longer function. I edit that question with a data frame so that I can understand. But I think this is more of a simpler problem than that. I am just having problem executing it. – Zo Wakaki Nov 12 '19 at 12:36

1 Answers1

0

In cases like this the solution is almost always to reshape the data from wide to long format.

economics %>%
  select(-date) %>%
  tidyr::gather(variable, value, -pop) %>%
  ggplot(aes(x = pop, y = value)) +
  geom_point(size = 0.5) +
  facet_wrap(~ variable, scales = "free_y")

economics %>%
  tidyr::gather(variable, value, -date) %>%
  ggplot(aes(y = value)) +
  geom_boxplot() +
  facet_wrap(~ variable, scales = "free_y")
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • Thanks this works. I had hard time understanding the 'reshaping the data'. I think i get that part now. But I still can't see which part of the code is causing it to repeat for the all the different columns. Edit: I see the repeat is actually done by tidyr::gather(variable, value, -pop) Part and -pop takes the x value and the variables are the ys. Thank you, this got me crystal. – Zo Wakaki Nov 12 '19 at 13:24
  • One more question. Lets say I have one more column like a population level. I made one using: economics=economics %>% mutate(pop_levels = case_when(pop < 224896 ~ "low", pop >= 224896 & pop <= 290291 ~ "medium", pop > 290291 ~ "high")) how would I use that for color on all of those graphs. i tried : economics %>% select(-date) %>% tidyr::gather(variable, value,-pop) %>% ggplot(aes(x = pop, y = value)) + geom_point(size = 0.5,col=pop_levels) + facet_wrap(~ variable, scales = "free_y") But that tells me it can't find pop_levels – Zo Wakaki Nov 12 '19 at 13:43
  • @ZoWakaki Try `tidyr::gather(variable, value,-pop, -pop_levels)` and then `ggplot(aes(x = pop, y = value, color = pop_levels)) + etc`. – Rui Barradas Nov 12 '19 at 16:29