【问题标题】:How to make dot plot with multiple data points for single variable?如何为单个变量制作具有多个数据点的点图?
【发布时间】:2021-06-12 19:38:55
【问题描述】:

我想为我的数据集创建点图。我知道如何使用 ggplot 为治疗比较或类似数据集创建正常的点图。我有以下数据。并想创建一个具有三种不同颜色的点图。请建议我如何为此点图准备数据。如果我在 NP 和 P 中有一个数据点,则很容易绘制,因为我已经使用过类似的数据,但对此类数据一无所知。我可以使用 R 中的 ggplot 模块并且可以完成。

变量 W 始终具有单个数据点,而 NP 和 P 具有不同的数据点,即在 NP 中有时为 1,有时在 NP 中为 3,与变量 P 相同,如表所示。

这是我的数据的屏幕截图。

对不起我的语言

我同意我的数据是一团糟。我用谷歌搜索并做了一些编码来获得情节。我使用 tidyverse 和 dplyr 包来实现绘图,但 y 轴再次出现问题。 Y轴非常笨拙。我使用了以下代码

d <- read.table("Data1.txt", header = TRUE, sep = "\t", stringsAsFactors = NA)
df <- data.frame(d)

df <- df %>%
 mutate(across(everything(), as.character)) %>%
 pivot_longer(!ID, names_to="colid", values_to="val") %>%
 separate_rows(val, sep="\t", convert=TRUE) %>%
 mutate(ID=as_factor(ID)

然后我用 ggplot 绘制图形

ggplot(df, aes(x=ID, y=val, color=colid))+geom_point(size=1.5) +theme(axis.text.x = element_text(angle = 90))

输出是这样的。我尝试使用 ylim 和 scale_y_discrete() 调整 Y 轴,但没有任何效果。请提出一种纠正方法。

【问题讨论】:

  • 听起来您需要重组数据。在二维散点图中,每个点都需要正好有 1 个 X 和 1 个 Y 值。如果您可以使用 dput(data) 分享您的数据,我们可能会提供更多帮助。

标签: r ggplot2


【解决方案1】:

这包含许多必要的数据清理步骤,正如用户 Dan Adams 在评论中所建议的那样。这很有趣,它帮助我拖延了自己的论文。

我正在使用来自一个非常著名的线程的函数,它提供了a way to splits columns when the number of resulting columns is unknown

附注您共享数据的方式不太理想。

#your data is unreadable without this awesome package
# devtools::install_github("alistaire47/read.so") 
library(tidyverse)
df <- read.so::read_md("|ID| |W| |NP| |P|

|:-:| |:-:| |:-:| |:-:|

|1| |4.161| |1.3,1.5| |1.5,2.8|

|2| |0.891| |1.33,1.8,1.79| |1.6|

|3| |7.91| |4.3| |0.899,1.43,0.128|

|40| |2.1| |1.4,0.99,7.9,0.32| |0.6,0.5,1.57|") %>%select(-starts_with("x")) 
#> Warning: Missing column names filled in: 'X2' [2], 'X4' [4], 'X6' [6]

# from this thread https://stackoverflow.com/a/47060452/7941188
split_into_multiple <- function(column, pattern = ", ", into_prefix){
  cols <- str_split_fixed(column, pattern, n = Inf)
  cols[which(cols == "")] <- NA
  cols <- as.tibble(cols)
  m <- dim(cols)[2]
  names(cols) <- paste(into_prefix, 1:m, sep = "_")
  cols
}
# apply this over the columns of interest
ls_cols <- lapply(c("NP", "P"), function(x) split_into_multiple(df$NP, pattern = ",", x))

# bind it to the single columns of the old data frame
# convert character columns to numeric
# apply pivot longer twice (there might be more direct options, but I won't be 
# bothered to do too much here)
df_new <- 
  bind_cols(df[c("ID", "W")], ls_cols) %>%
  pivot_longer(cols = c(-ID,-W), names_sep = "_", names_to = c(".value", "value")) %>%
  mutate(across(c(P, NP), as.numeric)) %>%
  select(-value) %>%
  pivot_longer(W:P, names_to = c("var"), values_to =  "value")

# The new tidy data can easily be plotted 
ggplot(df_new, aes(ID, value, color = var)) + 
  geom_point()
#> Warning: Removed 12 rows containing missing values (geom_point).

【讨论】:

  • 很抱歉我的数据混淆了,在这里我将标记我的数据的屏幕截图。我无法正确输入表格数据。抱歉弄乱了
  • @ThulasiR 实际上降价表更适合共享 - 屏幕截图确实是共享数据的最糟糕方式。检查stackoverflow.com/help/minimal-reproducible-example
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-30
  • 1970-01-01
  • 2023-03-26
相关资源
最近更新 更多