在 R 中更改小提琴图中的 x 轴
Change x-axis in violin plot in R
我正在尝试更改 vioplot 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)
但似乎 a 轴中的值被添加到我不想显示的 1-2-3 中。
谢谢
您的数据为 list() 格式,因此必须将其转换为数据框。然后通过将值堆叠在一起来融化数据框。
我们使用 geom_violin
创建核密度图,使用 geom_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
trim = 假
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
数据:
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)
我正在尝试更改 vioplot 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)
但似乎 a 轴中的值被添加到我不想显示的 1-2-3 中。
谢谢
您的数据为 list() 格式,因此必须将其转换为数据框。然后通过将值堆叠在一起来融化数据框。
我们使用 geom_violin
创建核密度图,使用 geom_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
trim = 假
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
数据:
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)