R 将对角线拟合到ggplot中的图形
我有一个像这样的数据框R 将对角线拟合到ggplot中的图形,r,ggplot2,R,Ggplot2,我有一个像这样的数据框 > wider_data # A tibble: 12 x 6 treat FR CC HP Other x <fct> <int> <int> <int> <int> <fct> 1 1 0 2 5 3 0 2 1 2 1 5 1 1 3 1
> wider_data
# A tibble: 12 x 6
treat FR CC HP Other x
<fct> <int> <int> <int> <int> <fct>
1 1 0 2 5 3 0
2 1 2 1 5 1 1
3 1 3 5 3 2 2
4 1 0 2 4 1 3
5 1 1 2 4 1 4
6 1 0 2 4 0 5
7 2 4 1 4 2 0
8 2 1 5 2 0 1
9 2 4 0 1 4 2
10 2 0 5 2 3 3
11 2 0 3 4 1 4
12 2 1 5 1 2 5
然后,我使用
但它不起作用
有没有办法做到这一点?提前谢谢你 也许这就是你想要的: 最好是转换数字。但是,将因子转换为数字时,必须记住,首先必须转换为字符。否则,第一个类别“0”变为1,第二个类别“1”变为2,依此类推。因此,始终使用
作为.numeric(作为.character(x))
在这次更改之后,您的代码应该可以正常工作。尽管如此,我还是做了一些小改动。我通过coord_cartesain设定了限制。我还使用expand参数来缩放x/y\u continuous将默认扩展(=5%)设置为0。顺便说一句:由于默认情况下ggplot将扩展您的轴,您不需要在限制中添加额外的“.05”。(如果您对默认扩展没有问题,只需删除scale\u x/y\u continuous
,或者如果您想要不同的扩展,请尝试使用c(0.025,0)
,它将轴两侧扩展2.5%。)
库(ggplot2)
图书馆(dpylr)
绘图测试%
ggplot(aes(x=as.numeric(as.character(x)),y=value,color=variable,group=variable))+
小关节包裹(变位(治疗),ncol=2)+
几何点()+
几何图形线(aes(线型=变量))+
几何线(斜率=1,截距=0)+
实验室(x=“x”,y=“y”)+
ggtitle('表')+
主题(legend.title=element\u blank())+
坐标笛卡尔(xlim=c(0,5),ylim=c(0,5))+
比例x连续(扩展=c(0,0))+
连续缩放(扩展=c(0,0))
曲线图检验
数据
m_wider_data <- read.table(text = "treat x variable value
1 1 0 FR 0
2 1 1 FR 2
3 1 2 FR 3
4 1 3 FR 0
5 1 4 FR 1
6 1 5 FR 0
7 2 0 FR 4
8 2 1 FR 1
9 2 2 FR 4
10 2 3 FR 0
11 2 4 FR 0
12 2 5 FR 1
13 1 0 CC 2
14 1 1 CC 1
15 1 2 CC 5
16 1 3 CC 2", header = TRUE)
m_wider_data$x <- factor(m_wider_data$x)
m_\u data我认为你的问题是因为x是一个因子而不是一个数字。最好在过程的早期执行转换。
此外,我还使用更现代的pivot\u long
更新了矩阵变换,而不是melt
widedf<-read.table(header = TRUE, text=" treat FR CC HP Other x
1 0 2 5 3 0
1 2 1 5 1 1
1 3 5 3 2 2
1 0 2 4 1 3
1 1 2 4 1 4
1 0 2 4 0 5
2 4 1 4 2 0
2 1 5 2 0 1
2 4 0 1 4 2
2 0 5 2 3 3
2 0 3 4 1 4
2 1 5 1 2 5")
library(tidyr)
#make the table long
df <- pivot_longer(widedf, -c("treat", "x"), names_to="variable", values_to="value")
#convert x from a character to an integer
df$x <- as.integer(as.character(df$x))
library(ggplot2)
plot_test <- df %>%
ggplot(aes(x = x, y = value, colour = variable, group = variable)) +
facet_wrap(vars(treat), ncol = 2) +
geom_point() +
geom_line(aes(linetype=variable)) +
geom_abline(slope = 1, intercept = 0) +
labs(x = "X", y = "Y") +
ggtitle('Table') +
theme(legend.title=element_blank()) #turns of the legend title
plot_test
widedfThanks也很多!这些图现在看起来很漂亮:)@JMV,如果这回答了你的问题,请看:
plot_test <- m_wider_data %>%
ggplot(aes(x = x, y = value, colour = variable, group = variable)) +
facet_wrap(vars(treat), ncol = 2) +
geom_point() +
geom_line(aes(linetype=variable)) +
geom_abline(slope = 1, intercept = 0) +
labs(x = "X", y = "Y") +
ggtitle('Table') +
theme(legend.title=element_blank()) #turns of the legend title
plot_test +
scale_x_continuous(breaks=c(0,1,2,3,4,5),limits = c(-0.05, 5.05)) +
scale_y_continuous(breaks=c(0,1,2,3,4,5),limits = c(-0.05, 5.05))
library(ggplot2)
library(dpylr)
plot_test <- m_wider_data %>%
ggplot(aes(x = as.numeric(as.character(x)), y = value, colour = variable, group = variable)) +
facet_wrap(vars(treat), ncol = 2) +
geom_point() +
geom_line(aes(linetype=variable)) +
geom_abline(slope = 1, intercept = 0) +
labs(x = "X", y = "Y") +
ggtitle('Table') +
theme(legend.title=element_blank()) +
coord_cartesian(xlim = c(0,5), ylim = c(0,5)) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0))
plot_test
m_wider_data <- read.table(text = "treat x variable value
1 1 0 FR 0
2 1 1 FR 2
3 1 2 FR 3
4 1 3 FR 0
5 1 4 FR 1
6 1 5 FR 0
7 2 0 FR 4
8 2 1 FR 1
9 2 2 FR 4
10 2 3 FR 0
11 2 4 FR 0
12 2 5 FR 1
13 1 0 CC 2
14 1 1 CC 1
15 1 2 CC 5
16 1 3 CC 2", header = TRUE)
m_wider_data$x <- factor(m_wider_data$x)
widedf<-read.table(header = TRUE, text=" treat FR CC HP Other x
1 0 2 5 3 0
1 2 1 5 1 1
1 3 5 3 2 2
1 0 2 4 1 3
1 1 2 4 1 4
1 0 2 4 0 5
2 4 1 4 2 0
2 1 5 2 0 1
2 4 0 1 4 2
2 0 5 2 3 3
2 0 3 4 1 4
2 1 5 1 2 5")
library(tidyr)
#make the table long
df <- pivot_longer(widedf, -c("treat", "x"), names_to="variable", values_to="value")
#convert x from a character to an integer
df$x <- as.integer(as.character(df$x))
library(ggplot2)
plot_test <- df %>%
ggplot(aes(x = x, y = value, colour = variable, group = variable)) +
facet_wrap(vars(treat), ncol = 2) +
geom_point() +
geom_line(aes(linetype=variable)) +
geom_abline(slope = 1, intercept = 0) +
labs(x = "X", y = "Y") +
ggtitle('Table') +
theme(legend.title=element_blank()) #turns of the legend title
plot_test