[英]Grouped boxplot for each column in R
我有一個這樣的數據集:
Y Gene1 Gene2 Gene3 Gene4 Gene...
TypeI 4.3621833 7.533461 3.9561242 4.457170 ...
TypeII 1.9844923 7.455194 5.4276231 5.440957 ...
TypeIII 1.7273229 8.079968 2.2273002 5.543480 ...
...
其中 Y 可以采用這三個值(有 150 行左右)。
現在,我正在嘗試為每個基因創建一個分組箱線圖(我會在 x 軸上有每個基因,每個基因有三個箱線圖)。
我幾乎讓它工作了,但是箱形圖出現在彼此之上,如下所示: 我試過在geom_boxplot()
中使用position_dodge()
和position_dodge2()
) 但都不起作用(它們實際上具有 0 效果)。
到目前為止,我已經嘗試了以下方法:
data %>%
pivot_longer(
cols = -Y,
names_to = "Genes",
values_to = "Value"
) %>%
ggplot(
aes(
x = Genes,
y = Value,
fill = Y
)
) +
geom_boxplot() +
labs(
title = "test"
)
和gather
等效:
data %>%
gather("Genes", "Valor", -Y) %>%
ggplot(
aes(
x = Genes,
y = Valor,
fill = Y
)
) +
geom_boxplot() +
labs(
title = "test"
) -> plot
plot <- ggplotly(plot, width = 2000, height = 1000)
saveWidget(plot, file = "boxplots.html")
我也嘗試按照這個答案用melt
來做,但無法讓它工作。
我可以對 plot 箱線圖並排做什么?
編輯:
所以經過一些測試我意識到 ggplot 的代碼工作正常。 我省略了一些代碼,因為我認為問題出在箱線圖中,但事實並非如此。
我添加了我省略的部分,但我也會把它放在這里:
data %>%
(....) -> plot
plot <- ggplotly(plot, width = 2000, height = 1000)
saveWidget(plot, file = "boxplots.html")
所以箱形圖在 ggplot 中沒有重疊,但在 plotly 中。在 ggplotly() 修復它之后添加%>%layout(boxmode = "group")
。 (ie plot <- ggplotly(plot, width = 2000, height = 1000) %>% layout(boxmode = "group")
正如我在上次編輯中所說,ggplot 代碼是正確的。 問題出在這一行:`plot <- ggplotly(plot, width = 2000, height = 1000)。
在 ggplotly() 修復它之后添加%>%layout(boxmode = "group")
。 (ie plot <- ggplotly(plot, width = 2000, height = 1000) %>% layout(boxmode = "group")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.