-2

So I am trying to make two boxplots on one graph of two separate variables. I have a dataset with multiple variables but I wanna compare only two: income_husband, and income_wife. I have done it using boxplot() but how can i do it using ggplot ?

koras
  • 15
  • 5
  • Please make this question [easier to answer](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including some example data. – neilfws Oct 15 '18 at 00:10
  • Possibly by programming. Your first question on SO had a similar recommendation as ^^ w/r/t framing a better question. You did not interact with the contributor for your second question. Please consider and respect the time of contributors by following the recommendations for posting questions that you received during the question creation process and in the SO R FAQ. – hrbrmstr Oct 15 '18 at 00:22

1 Answers1

0

It would help if you had some data to work with but I have put some sample data together. Group C is filtered out. Is this what you are sort of after?

library(tidyverse)

group = c("a", "a", "a", "a", "a", "b", "b", "b", "b", "b", 'c', "c")
income = c(100, 120, 110, 23, 34, 120, 45, 156, 65, 52, 65, 98)

data <- tibble(group, income)
data

data2 <- data %>% 
  filter(group == "a" | group == "b" )

b <- ggplot(data2, aes(x = group, y = income)) 
b + geom_boxplot()
william3031
  • 1,653
  • 1
  • 18
  • 39
  • I'm not sure introducing the vast, compiled C++ & multi-dozen package set of dependencies that come with the `tidyverse` is necessary to just build a data frame for use in ggplot2. – hrbrmstr Oct 15 '18 at 00:23
  • It is just to get the tibble to work. There isn't any data so I put something together. I could have done it via a dataframe. But since a ggplot is being used, I thought the tidyverse was appropriate. Feedback taken though. – william3031 Oct 15 '18 at 00:27
  • 1
    From the question it sounds as though the groups (husband and wife income) are in two different columns. So this is probably a classic "wide to long" problem where `tidyr::gather` could be used before `ggplot`. – neilfws Oct 15 '18 at 00:32
  • @neilfws, yes probably. – william3031 Oct 15 '18 at 02:02