0

I would like one category per faceted box plot. Instead I am currently getting distribution points for all categories in each faceted box.

Category   Age
A          31
A          35
A          28
B          34
B          30
B          40
C          22
C          25
C          24

ggplot(DATASET, aes(x = DATASET$Category, y = DATASET$Age)) +
    geom_point() +
    ggtitle('Distribution of Player Age By Category') +
    facet_grid( .~DATASET$Category )
halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

1

There is no real need for facets here other than aesthetics (see @LAP's answer), but if you insist on using facets you can do

ggplot(df, aes(x = Category, y = Age)) +
    geom_point() +
    ggtitle('Distribution of Player Age By Category') +
    facet_wrap(~ Category, scales = "free_x")

enter image description here

More importantly, never use $ (column indexing) inside aes. This can lead to very unexpected behaviour, in particular when using facets.

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
0

No need to facet for boxplotting with ggplot2:

df <- read.table(text = "Category   Age
A          31
A          35
A          28
B          34
B          30
B          40
C          22
C          25
C          24", header = TRUE)

ggplot(df, aes(x = Category, y = Age)) +
  geom_boxplot() +
  ggtitle('Distribution of Player Age By Category')

enter image description here

LAP
  • 6,605
  • 2
  • 15
  • 28