R ggplot2 boxplot not properly shown when knitting html

587 views Asked by At

tried a simply boxplot with ggplo2

ggplot(aes(x = quality, y = residual.sugar),data=data)+
  geom_boxplot(fill="#9999CC")+scale_y_continuous(limits =c (0,20))

when i exucute it in R itself, it looks like want it , like this: enter image description here

but when i knit it to get my html file, it looks like this :

enter image description here

Anyone knows how to fix that? i am confused :(

1

There are 1 answers

0
Ben Bolker On BEST ANSWER

The most likely difference is that you forgot to make quality into a factor within your rmd file.

For example:

set.seed(101)
dd <- data.frame(quality = sample(6:9,size=200,replace=TRUE),
                 residual.sugar = rnorm(200))

library(ggplot2)
ggplot(aes(x = quality, y = residual.sugar),data=dd)+
   geom_boxplot()

enter image description here

dd2 <- transform(dd,quality=factor(quality))
ggplot(aes(x = quality, y = residual.sugar),data=dd2)+
   geom_boxplot()

enter image description here