gpt4 book ai didi

r - 在来自不同组的两个点之间画一条线,同时跟踪这些点

转载 作者:行者123 更新时间:2023-12-03 17:20:18 25 4
gpt4 key购买 nike

我正在尝试比较条件 x 与 y 中的值。这是我的数据:

mydata <-  structure(list(
Row.names = c("ASPTAm", "ATPtmB_MitoCore", "CI_MitoCore",
"CIII_MitoCore", "CIV_MitoCore", "CO2t", "CO2tm", "CV_MitoCore",
"H2Ot", "H2Otm", "MDH", "MDHm", "O2t", "O2tm", "OF_ATP_MitoCore",
"PGK", "PGM", "PIt2mB_MitoCore", "SUCOASm"),
mean_x = c(-1.416333333,
26.1376024, 8.444222444, 9.983111555, 4.991555778, -5.06, -5.055,
24.43926907, -4.719, -30.9051024, -1.580333333, 3.093666667,
5, 5, 29.7476024, -1.81, -1.81, 25.9436024, -1.698333333),
mean_y = c(-2.455e-14,
78.68825722, 51.30062794, 9.897398744, 4.948699372, -40.05114286,
-40.15114286, 68.14247151, -29.51685714, -108.3586144, -60.164,
10.90278571, 5, 5, 82.39825722, -1.81, -1.81, 86.41154294, -10.58878571
)),
class = "data.frame", row.names = c(NA, -19L),
.Names = c("Row.names", "mean_x", "mean_y"))

我想要一个点图,每组之间有一条线连接。这是我尝试过的:
library(reshape2)
library(ggplot2)

mydata <- melt(mydata)

ggplot(mydata, aes(x = Row.names, y = value, color = variable)) +
geom_point(stat = 'identity', position = position_dodge(width = 1.0), size = 2.5) +
theme(axis.text.x = element_text(angle = 60, hjust = 1))

这是我得到的情节:

img from link 1

我还想连接每个变量中的两个点。
如果我按 Row.names 对数据进行分组,然后添加 geom_line() ,它有效,但每组中的点再次重叠。

img form link 2

连接点时如何分开点?

最佳答案

实现此目的的一种方法是使用 %.>%来自 wrapr 的管道专门为 geom_*() 打包和 reshape 数据.

library(tidyverse)
library(wrapr)

mydata %>%
mutate(
x = row_number(),
more = mean_x < mean_y,
x1 = if_else(more, x - .1, x + .1),
x2 = if_else(more, x + .1, x - .1)
) %.>%
ggplot() +
geom_segment(
data = .,
aes(
x = x1,
xend = x2,
y = mean_x,
yend = mean_y
),
color = 'grey'
) +
geom_point(
data = unite(., 'mean_x', c('mean_x', 'x1')) %>%
unite('mean_y', c('mean_y', 'x2')) %>%
gather(variable, value, mean_x:mean_y) %>%
separate(value, c('y', 'x'), sep = '_') %>%
mutate_at(4:5, as.numeric),
aes(
x = x,
y = y,
color = variable
)
) +
ylab('value') +
xlab('Row.names') +
scale_x_continuous(
breaks = .$x,
labels = .$Row.names
) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 60, hjust = 1))

enter image description here

关于r - 在来自不同组的两个点之间画一条线,同时跟踪这些点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54230346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com