Home > Mobile >  Violin plot of ggplot2 requires an x variable even though there is none
Violin plot of ggplot2 requires an x variable even though there is none

Time:11-04

I'm trying to create a violin plot with the continuous variable tm_aerobic_vario which has the following values:

NA 50 614 30 60 180 120 240 12 60 135 120 25 120 120 60 10 120 90 90 30 0 180 120 30 5 0 0 60 180 120 24 30 30 40 40 0 180 60 180 45 0 10 30 120 120 1 45 15 30 180

The code is:

ggplot(initdata, aes(x = 1, y = tm_aerobic_vario)) geom_violin(trim=FALSE,fill="gray") geom_boxplot(width=0.1) theme_apa()

It keeps insisting that it needs an x value (in another dataset, I used x as 'Group' and it made a nice graph with two groups in the x axis) although there is no x value for me to provide. Thus the graph below

enter image description here

What can I do to replace the X axis, or which value is the correct to provide in case of only 1 variable?

CodePudding user response:

As neilfws wrote, x is a factor, so you can just leave it blank by writing x = ""

so

ggplot(initdata, aes(x = "", y = tm_aerobic_vario)) geom_violin(trim=FALSE,fill="gray") geom_boxplot(width=0.1) theme_apa()

CodePudding user response:

I add it as an complete example below. You can edit the axis even more if you want to get rid of it completely.

library(ggplot2)

initdata <- data.frame(tm_aerobic_vario = c(NA, 50, 614, 30, 60, 180, 120, 240, 12, 60, 135, 120, 25, 120, 120, 60, 10, 120, 90, 90, 30, 0, 180, 120, 30, 5, 0, 0, 60, 180, 120, 24, 30, 30, 40, 40, 0, 180, 60, 180, 45, 0, 10, 30, 120, 120, 1, 45, 15, 30, 180))

ggplot(initdata, aes(x = "tm aerobic vario", y=tm_aerobic_vario))  
  labs(x="", y="")  
  geom_violin(trim = F, fill = "gray")   
  geom_boxplot(width=0.1)  
  theme_classic()  
  # theme(axis.text = element_blank(), axis.ticks.x = element_blank()) # to get rid of axis completely

enter image description here

  • Related