2017-03-20 36 views
1

我正在嘗試更改vioplot的x軸中的值。我用了一個建議,並寫道:更改R的小提琴圖中的x軸

library(vioplot) 
labels=c(10,20,30) 
x1=c(1,2,3,4) 
x2=c(5,6,7,8,9,10) 
x3=c(11,12,13,14,15,16) 
x=list(x1,x2,x3) 
do.call(what = vioplot, args = x) 
axis(side=1,at=1:length(labels),labels=labels) 

但似乎在一個軸上的值被添加到1-2-3,我不希望被提出。

謝謝

+0

是的。但前提是我可以通過小提琴中的數值列表,而不是分別指定每個級別。 – user552231

回答

1

您在列表中有你的數據()格式,所以它必須被轉換成數據幀。然後通過將數值堆疊在一起來融化數據幀。

使用geom_violin我們創建了內核密度圖,使用geom_boxplot,我們在內核密度圖上創建了箱圖。 boxplot的寬度是使用width控制的。

library('ggplot2') 
library('reshape2') 
df <- data.frame(lapply(x, function(y) {length(y) <- max(lengths(x)); y})) # create data frame from list of x 
colnames(df) <- as.character(labels) # change column names to labels 
df <- melt(df)      # melt data frame 
df <- df[ !is.na(df$value), ]   # remove NA 
ggplot(data = df) + 
    geom_violin(aes(x = variable, y = value, fill = variable)) + # kernel density plot 
    geom_boxplot(aes(x = variable, y = value), width = 0.1) + # box plot 
    xlab(" labels ") + # x axis title 
    ylab(" values ")  # y axis title 

enter image description here

裝飾= FALSE

ggplot(data = df) + 
    geom_violin(aes(x = variable, y = value, fill = variable), trim = FALSE) + # kernel density plot 
    geom_boxplot(aes(x = variable, y = value), width = 0.1) + # box plot 
    xlab(" labels ") + # x axis title 
    ylab(" values ")  # y axis title 

enter image description here

數據:

labels=c(10,20,30) 
x1=c(1,2,3,4) 
x2=c(5,6,7,8,9,10) 
x3=c(11,12,13,14,15,16) 
x=list(x1,x2,x3)