简体   繁体   English

在 R 中绘制小​​提琴图

[英]Plotting violin plot in R

I am trying to create a violin plot using a data frame in the long format.我正在尝试使用长格式的数据框创建小提琴图。 The data frame has 2 columns headed group (containing 2 factors- efficient and inefficient) and Glucose m+6 with corresponding numerical values.数据框有 2 列标题组(包含 2 个因素 - 有效和无效)和葡萄糖 m+6 以及相应的数值。

I have tried plotting a violin plot using the following code:我尝试使用以下代码绘制小提琴图:

Dta_lng %>% 
  ggplot(aes(x= Group, y= `Glucose m+6`, fill= Group)) +
  geom_violin(show.legend = FALSE) +
  geom_jitter(aes(fill=Group),width=0.1, alpha=0.6, pch=21, color="black")

This is the resulting plot: https://i.stack.imgur.com/VrtbU.jpg这是结果图: https : //i.stack.imgur.com/VrtbU.jpg

The console also gives 50 warning messages saying groups with fewer than two data points have been dropped.控制台还给出了 50 条警告消息,说明数据点少于两个的组已被删除。

Perhaps like this:也许像这样:

Data: (Note the different labels!)数据:(注意不同的标签!)

df <- data.frame(
  group = c(sample(c("efficient", "inefficient"), 1000, replace = TRUE)),
  Glucose_m_6 = rnorm(1000) 
)

The violin plot with scatter plot:带有散点图的小提琴图:

ggplot(data = df, 
       aes(x = group, y = Glucose_m_6, fill = group)) +
  geom_violin(scale = "count", trim = F, adjust = 0.7, kernel = "cosine") +
  geom_point(aes(y = Glucose_m_6), 
             position = position_jitter(width = .25), size = 0.9, alpha = 0.8)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM