0

Im trying to make a box and whisker plot in ggplot but I cant seem to make it work.

My data is this below. I can make it just fine using the default boxplot function but not ggplot. The thing is it wants an x and y but I don't have a y axis besides a value.

boxplot(data1) works just fine and its too simple to mess up.

enter image description here

0

1 Answer 1

0

In order to use the boxplot function, you must first pivot longer so your data looks like

library(tidyverse)

# generating random data
data <- tibble(
  before = runif(30, 30, 40),
  after = runif(30, 35, 45)
)

(data <- data %>%
  pivot_longer(everything(), names_to = "time", values_to = "value"))
#> # A tibble: 60 × 2
#>    time   value
#>    <chr>  <dbl>
#>  1 before  36.0
#>  2 after   36.7
#>  3 before  32.4
#>  4 after   38.7
#>  5 before  33.1
#>  6 after   44.7
#>  7 before  39.0
#>  8 after   36.4
#>  9 before  39.6
#> 10 after   43.8
#> # ℹ 50 more rows

Then, you can use geom_boxplot

ggplot(data) +
  geom_boxplot(aes(x = time, y = value))

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.