Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在ggplot2中手动向x轴和y轴添加打断_R_Ggplot2_Axis_Scatter Plot - Fatal编程技术网

R 在ggplot2中手动向x轴和y轴添加打断

R 在ggplot2中手动向x轴和y轴添加打断,r,ggplot2,axis,scatter-plot,R,Ggplot2,Axis,Scatter Plot,我通常使用scale\u y\u continuous(或scale\u x\u continuous)更改ggplot2中连续变量的分界符。我还通常使用坐标笛卡尔(xlim=)(或y轴的ylim=)设置轴限制。所有这些都可以正常工作,如下所示: #Some example data set.seed(100) b<-sample(1:10, 10) a<-sample(1:10, 10) df<-data.frame(a,b) #Graph without scales

我通常使用
scale\u y\u continuous
(或
scale\u x\u continuous
)更改ggplot2中连续变量的分界符。我还通常使用坐标笛卡尔(xlim=)(或y轴的
ylim=
)设置轴限制。所有这些都可以正常工作,如下所示:

#Some example data
set.seed(100)
b<-sample(1:10, 10)
a<-sample(1:10, 10)

df<-data.frame(a,b)

#Graph without scales set (just default)
library(ggplot2)
ggplot(data=df, aes(a,b))+
  theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
                     panel.grid.major = element_blank(), 
                     panel.grid.minor = element_blank(), 
                     axis.line = element_line(colour = "black"))+
  geom_text(aes(label=rownames(df)), color="black")
ggplot(data=df, aes(a,b))+
  theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
                     panel.grid.major = element_blank(), 
                     panel.grid.minor = element_blank(), 
                     axis.line = element_line(colour = "black"))+
  geom_text(aes(label=rownames(df)), color="black")+
  scale_x_continuous(breaks = c(1,3,5,7,9,11,13))+
  coord_cartesian(xlim=c(0, 13))

…对于y轴

ggplot(data=df, aes(a,b))+
  theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
                     panel.grid.major = element_blank(), 
                     panel.grid.minor = element_blank(), 
                     axis.line = element_line(colour = "black"))+
  geom_text(aes(label=rownames(df)), color="black")+
  scale_y_continuous(breaks = c(1,3,5,7,9,11,13))+
  coord_cartesian(ylim=c(0, 13))

但是,当我试图同时完成两个轴的相同比例调整时,输出不会产生我期望的结果(在x轴和y轴上都是1-13)

如您所见,x轴和y轴并不相等,即使向两个轴传递了完全相同的代码。我不明白为什么。非常感谢您的帮助。

set.seed(100)
set.seed(100)
df <- data.frame(b = sample(1:10, 10), a = sample(1:10, 10))    


ggplot(data=df, aes(a,b))+
theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
                   panel.grid.major = element_blank(), 
                   panel.grid.minor = element_blank(), 
                   axis.line = element_line(colour = "black")) +
geom_text(aes(label=rownames(df)), color="black") +
scale_y_continuous(breaks = c(1,3,5,7,9,11,13)) +
scale_x_continuous(breaks = c(1,3,5,7,9,11,13)) +
coord_fixed(ylim=c(0, 13),xlim=c(0, 13))

df
scale.*.\u continuous
函数具有一个
限制
参数,您可以使用该参数代替笛卡尔坐标

ggplot(data=df, aes(a,b))+
    theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
                       panel.grid.major = element_blank(), 
                       panel.grid.minor = element_blank(), 
                       axis.line = element_line(colour = "black"))+
    geom_text(aes(label=rownames(df)), color="black")+
    scale_y_continuous(breaks = c(1,3,5,7,9,11,13), limits = c(0, 13)) +
    scale_x_continuous(breaks = c(1,3,5,7,9,11,13), limits = c(0, 13))

…或者,如果您坚持使用
coord\u cartesian
,请在一次呼叫中完成。否则,它会裁剪图形,然后再次裁剪,这就是导致问题的原因

ggplot(data=df, aes(a,b))+
    theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
                       panel.grid.major = element_blank(), 
                       panel.grid.minor = element_blank(), 
                       axis.line = element_line(colour = "black"))+
    geom_text(aes(label=rownames(df)), color="black")+
    scale_y_continuous(breaks = c(1,3,5,7,9,11,13))+
    scale_x_continuous(breaks = c(1,3,5,7,9,11,13))+
    coord_cartesian(xlim=c(0, 13), ylim = c(0, 13))
# returns the same thing

不确定,但我忘了添加一行代码(我的坏代码),只添加了用于收集
a
b
的数据帧:
dfahh我看到它的
坐标固定了
谢谢!
ggplot(data=df, aes(a,b))+
    theme_bw() + theme(panel.border = element_rect(colour = "black", fill=NA, size=1), 
                       panel.grid.major = element_blank(), 
                       panel.grid.minor = element_blank(), 
                       axis.line = element_line(colour = "black"))+
    geom_text(aes(label=rownames(df)), color="black")+
    scale_y_continuous(breaks = c(1,3,5,7,9,11,13))+
    scale_x_continuous(breaks = c(1,3,5,7,9,11,13))+
    coord_cartesian(xlim=c(0, 13), ylim = c(0, 13))
# returns the same thing