Fasceted Bloxplot - 值/数据结构

问题描述 投票:0回答:1

我很难使箱线图的数据符合我想要显示的内容。 基本上,我希望“肿瘤”的第一个 fasceted 箱线图的第一个箱线图的值为 2.97 和 2.97,第二个箱线图的值为 2.9 和 2.97,第三个箱线图的值为 2.6(只是一条线)。 对于“邻近肺”,1.5 和 2.25; 2.97 和 2.5;和2.37。 而对于“远离肿瘤的组织”,2.5和2.8; 2.5 和 2.1;和 3.

我不知道为什么它在图表中表现得很奇怪(见图)。 我抖动它以便我能清楚地看到要点。

不确定这里使用什么数据结构。请帮忙。

参见下面的代码:

x <- c(2.97, 2.97, 1.5, 2.25, 2.5, 2.8)
y <- c(2.9, 2.97, 2.97, 2.5, 2.5, 2.1)
z <- c(2.6, 2.97, 3)

data <- data.frame(
  values = c(x, y, z),
  group = factor(
    c(rep("y", length(y)), rep("x", length(x)), rep("z", length(z))),
    levels = c("x", "y", "z")
  ),
  position = factor(
    rep(c("tumor", "adjacent lung", "tissue far from tumor"), length(x) + length(y) + length(z)),
    levels = c("tumor", "adjacent lung", "tissue far from tumor")
  )
)

library(ggplot2)

ggplot(data, aes(x = group, y = values, fill = position)) + 
  geom_boxplot() +
  geom_point(aes(fill=group), size=5, shape=21, colour="grey20",
             position=position_jitter(width=0.2, height=0.1)) +
  scale_x_discrete(labels = c("x" = "Case 1, 4", "y" = "Case 2, 3", "z" = "Case 5")) +
  labs(title = "IL-13 Receptor alpha 1 positivity scores",
       x = "Patient Cases", y = "Positivity scores") +
  theme(axis.text.x = element_text(face = "bold", color = "black", size = 12, angle = 45),
        axis.text.y = element_text(face = "bold", color = "black", size = 12, angle = 45)) +
  facet_wrap(~ position)

我尝试输入 10 作为值只是为了看看会发生什么,但我仍然不确定问题是什么。

enter image description here

r ggplot2
1个回答
0
投票

您有 15 个数据点(12 x、12 y 和 3z),但它们在

data
中是一式三份,并且
x
y
值似乎交换了。

看:

> distinct(data) %>% arrange(group)
   values group              position
1    2.90     x                 tumor
2    2.97     x         adjacent lung
3    2.97     x tissue far from tumor
4    2.50     x                 tumor
5    2.50     x         adjacent lung
6    2.10     x tissue far from tumor
7    2.97     y                 tumor
8    2.97     y         adjacent lung
9    1.50     y tissue far from tumor
10   2.25     y                 tumor
11   2.50     y         adjacent lung
12   2.80     y tissue far from tumor
13   2.60     z                 tumor
14   2.97     z         adjacent lung
15   3.00     z tissue far from tumor

使用

dplyr::distinct(data)
绘图将为您提供下图:这是否更有意义?

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.