繁体   English   中英

R图:在图外操作轴上的小数位数和图例大小

[英]R plot: manipulating decimal digits on axis and legend size outside the plot

亲爱的社区! 使用R绘图使我的绘图完美漂亮时,我遇到了一些问题。 我对此主题进行了大量研究,但无法解决最后剩下的问题(此外,我对RStudio和一般编码还很陌生)。 我有这个情节plot_example是用下面的代码创建的:

df1 <- read.csv2("df1.csv", header=T, sep=";", dec=".")
el_x1 <- df1$element
el_y1 <- df1$ratio
df2 <- read.csv2("df2.csv", header=T, sep=";", dec=".")
el_x2 <- df2$element
el_y2 <- df2$ratio
df3 <- read.csv2("df3.csv", header=T, sep=";", dec=".")
el_x3 <- df3$element
el_y3 <- df3$ratio
df4 <- read.csv2("df4.csv", header=T, sep=";", dec=".")
el_x4 <- df4$element
el_y4 <- df4$ratio
df5 <- read.csv2("df5.csv", header=T, sep=";", dec=".")
el_x5 <- df5$element
el_y5 <- df5$ratio
df6 <- read.csv2("df6.csv", header=T, sep=";", dec=".")
el_x6 <- df6$element
el_y6 <- df6$ratio
df7 <- read.csv2("df7.csv", header=T, sep=";", dec=".")
el_x7 <- df7$element
el_y7 <- df7$ratio
df8 <- read.csv2("df8.csv", header=T, sep=";", dec=".")
el_x8 <- df8$element
el_y8 <- df8$ratio
df9 <- read.csv2("df9.csv", header=T, sep=";", dec=".")
el_x9 <- df9$element
el_y9 <- df9$ratio
df10 <- read.csv2("df10.csv", header=T, sep=";", dec=".")
el_x10 <- df10$element
el_y10 <- df10$ratio
df11 <- read.csv2("df11.csv", header=T, sep=";", dec=".")
el_x11 <- df11$element
el_y11 <- df11$ratio
par(fin = c (5,5), mar = c(5,5,6,1), xpd = "true")
plot (xlim = c(0.1,250), ylim = c(0.003,4), el_x1, el_y1, log = "xy", xlab = "element (ppm)", ylab = "ratio", pch = 0, cex = 0.7, col = "orange1", las = 1, cex.axis = 1, cex.lab = 1.8, yaxs = "i", xaxs = "i")
points (el_x2, el_y2, pch = 3, cex = 0.7, col = "olivedrab2", las = 1, yaxs = "i", xaxs = "i")
points (el_x3, el_y3, pch = 4, cex = 0.7, col = "darkseagreen1", las = 1, yaxs = "i", xaxs = "i")
points (el_x4, el_y4, pch = 2, cex = 0.7, col = "khaki1", las = 1, yaxs = "i", xaxs = "i")
points (el_x5, el_y5, pch = 1, cex = 0.7, col = "lavender", las = 1, yaxs = "i", xaxs = "i")
points (el_x6, el_y6, pch = 15, cex = 0.7, col = "seagreen", las = 1, yaxs = "i", xaxs = "i")
points (el_x7, el_y7, pch = 15, cex = 1, col = "indianred1", las = 1, yaxs = "i", xaxs = "i")
points (el_x8, el_y8, pch = 16, cex = 1, col = "royalblue1", las = 1, yaxs = "i", xaxs = "i")
points (el_x9, el_y9, pch = 15, cex = 1, col = "red2", las = 1, yaxs = "i", xaxs = "i")
points (el_x10, el_y10, pch = 16, cex = 1, col = "navy", las = 1, yaxs = "i", xaxs = "i")
points (el_x11, el_y11, pch = 16, cex = 1, col = "darkmagenta", las = 1, yaxs = "i", xaxs = "i")
legend ("topright", inset=c(-0.46,0), legend=c("df1verylongname", "df2verylongname", "df3verylongname", "df4verylongname", "df5verylongname", "df6verylongname", "df7verylongname","df8verylongname", "df9verylongname", "df10verylongname", "df11verylongname"), pch=c(0,3,4,2,1,15,15,15,16,16,16), col=c("orange1","olivedrab2","darkseagreen1","khaki1", "lavender", "seagreen", "indianred1", "red2", "royalblue2", "navy", "darkmagenta")) 

现在,我想作一些修改。

  1. (最重要)我必须操纵两个轴上的小数位。 我想保留0.005,但是我想摆脱50.0(应该是:50)。 我还无法使用“ sprintf”正确地操作它。 那么,如何删除所有不需要的.0xxx数字? 如果还有其他解决方案,而不是使用图片编辑程序来处理情节,那将是很好的...
  2. 我可以将图例的大小调整为剧情的高度吗? 它应该从右侧框的底部到顶部(在这种情况下,从y值0.003到4)到达。
  3. 除了“ xpd = true”之外,还有另一种方法可以将图例放置在绘图旁边吗? 因为我想摆脱一些似乎离群的数据点。 当然,我可以调整图的大小或删除异常值,但是如果框外的点未如一般/在添加xpd之前那样总体上显示出来,则会更容易。
  4. 是否有一种很好的方法可以在轴数字和轴标签之间获得更多的空间? 要使绘图达到这样的大小非常困难,并且我尝试调整空间会破坏所有内容,我并不是真的想使轴的数字更小... :( :(

这是一些输入数据,我将df简化为必要的东西:

sample  element ratio
1   0.6175  0.063568046
2   4.678133548 0.008924568
3   3.638120051 0.005707297
4   4.116887372 0.007867378
5   3.090387742 0.059814081
6   10.3098635  0.040600128
7   10.29649952 0.019962218
8   8.241753356 0.012910088
9   26.76850701 0.294731393
10  8.136495793 0.031161747
11  7.993317894 0.021593337
12  10.48361696 0.025802074
13  14.37169851 0.030482194
14  13.19394369 0.032786504
15  3.756483892 0.027182974
16  12.21848391 0.032594756
17  6.560291802 0.049019108
18  10.71912771 0.072430938
19  1.708155726 0.007771124
20  10.0684893  0.054408104
21  16.38043254 0.076730258
22  11.50283707 0.044073631
23  10.11554913 0.064254448
24  13.52364219 0.0707148
25  10.51594695 0.060657727
26  14.51045122 0.07431958
27  16.46248665 0.059498267
28  12.17582522 0.04927026
29  20.83513461 0.059772704
30  18.10278099 0.069570053
31  8.404223175 0.051083018
32  13.96869871 0.069607707
33  9.396932425 0.044899193
34  12.39775022 0.066800258
35  1.625963393 0.009627417
36  16.9934813  0.059799741
37  75.5    0.3775
38  73  0.350961538
39  45.3    0.191949153
40  92.5    0.409292035
41  39.8    0.176106195
42  48.2    0.194354839
43  48.9    0.188076923
44  52.6    0.219166667
45  44.4    0.1734375
46  53.9    0.245
47  168.4   0.559468439
48  165.8   0.587943262
49  87.9    0.412676056
50  99.5    0.328382838
51  105.9   0.318975904
52  172.7   0.579530201
53  190.4   0.602531646
54  206.6   0.586931818
55  101 0.321656051
56  101.8   0.329449838
57  117.7   0.346176471
58  97.9    0.337586207
59  90.5    0.285488959
60  182.1   0.550151057
61  109.9   0.399636364
62  8   0.053619303
63  4.9 0.039294306
64  4.7 0.035768645
65  6.3 0.041888298
66  8.3 0.035698925
67  6.7 0.043733681
68  25.2    0.193548387
69  24.7    0.168600683
70  59.1    0.226610429
71  30.6    0.234303216
72  45.1    0.223710317
73  93.2    0.315611243
74  83.1    0.309151786
75  88.8    0.311360449
76  34.8    0.169014085
77  36.6    0.183550652
78       
79  4.47    0.071565802
80  1.62    0.012413793
81  2.08    0.021010101
82  1.27    0.012330097
83  1.34    0.019705882
84  2.54    0.030274136
85  3.96    0.057641921
86  32.9    0.115438596
87  19.4    0.067595819
88  27.6    0.088461538
89  14.5    0.050699301
90  31  0.096875
91  36.3    0.078232759
92  27.8    0.072395833
93  8.56    0.039447005
94  10.1    0.040239044
95  4.44    0.056060606
96  3.94    0.05317139
97  4.18    0.059123055
98  5.23    0.07568741

如果需要更多输入数据,请联系。 如果有人能在这种情况下帮助我,我会很高兴。 问候!

这是ggplot2的解决方案。 它不能满足您的所有要求,因为您仅提供了1个元素的数据,而我还没有完全理解所有要求。 轴标签是荒谬的,但是您明白了。

library(ggplot2)

df <- read.table(stringsAsFactors = FALSE, header = TRUE, text = "sample  element ratio
1   0.6175  0.063568046
2   4.678133548 0.008924568
3   3.638120051 0.005707297
4   4.116887372 0.007867378
5   3.090387742 0.059814081
6   10.3098635  0.040600128
7   10.29649952 0.019962218
8   8.241753356 0.012910088
9   26.76850701 0.294731393
10  8.136495793 0.031161747
11  7.993317894 0.021593337
12  10.48361696 0.025802074
13  14.37169851 0.030482194
14  13.19394369 0.032786504
15  3.756483892 0.027182974
16  12.21848391 0.032594756
17  6.560291802 0.049019108
18  10.71912771 0.072430938
19  1.708155726 0.007771124
20  10.0684893  0.054408104
21  16.38043254 0.076730258
22  11.50283707 0.044073631
23  10.11554913 0.064254448
24  13.52364219 0.0707148
25  10.51594695 0.060657727
26  14.51045122 0.07431958
27  16.46248665 0.059498267
28  12.17582522 0.04927026
29  20.83513461 0.059772704
30  18.10278099 0.069570053
31  8.404223175 0.051083018
32  13.96869871 0.069607707
33  9.396932425 0.044899193
34  12.39775022 0.066800258
35  1.625963393 0.009627417
36  16.9934813  0.059799741
37  75.5    0.3775
38  73  0.350961538
39  45.3    0.191949153
40  92.5    0.409292035
41  39.8    0.176106195
42  48.2    0.194354839
43  48.9    0.188076923
44  52.6    0.219166667
45  44.4    0.1734375
46  53.9    0.245
47  168.4   0.559468439
48  165.8   0.587943262
49  87.9    0.412676056
50  99.5    0.328382838
51  105.9   0.318975904
52  172.7   0.579530201
53  190.4   0.602531646
54  206.6   0.586931818
55  101 0.321656051
56  101.8   0.329449838
57  117.7   0.346176471
58  97.9    0.337586207
59  90.5    0.285488959
60  182.1   0.550151057
61  109.9   0.399636364
62  8   0.053619303
63  4.9 0.039294306
64  4.7 0.035768645
65  6.3 0.041888298
66  8.3 0.035698925
67  6.7 0.043733681
68  25.2    0.193548387
69  24.7    0.168600683
70  59.1    0.226610429
71  30.6    0.234303216
72  45.1    0.223710317
73  93.2    0.315611243
74  83.1    0.309151786
75  88.8    0.311360449
76  34.8    0.169014085
77  36.6    0.183550652
79  4.47    0.071565802
80  1.62    0.012413793
81  2.08    0.021010101
82  1.27    0.012330097
83  1.34    0.019705882
84  2.54    0.030274136
85  3.96    0.057641921
86  32.9    0.115438596
87  19.4    0.067595819
88  27.6    0.088461538
89  14.5    0.050699301
90  31  0.096875
91  36.3    0.078232759
92  27.8    0.072395833
93  8.56    0.039447005
94  10.1    0.040239044
95  4.44    0.056060606
96  3.94    0.05317139
97  4.18    0.059123055
98  5.23    0.07568741")

# 
ggplot(df, aes(element, ratio, fill="Element 1")) +
    geom_point(color="red2") +
    #geom_point(color=factor(1), size=1) 
    coord_cartesian( xlim=c(0, 300), ylim=c(0.0003, 4)) +
    scale_x_continuous(labels=c(0, "hundred", 200, ">=300"), breaks=c(0, 100, 200, 300)) +
    labs(x = "X axis", y = "Y axis (ratio/ppm)", fill = "Elements") +
    theme_minimal() +
    theme(#legend.position = "right", 
          legend.position = c(0.05, 0.95), 
          legend.justification = c(0.05, 0.95),
          axis.title.x = element_text(color="black", hjust = 0),
          axis.title.y = element_text(color="black", hjust = 0))

情节

我无法真正解决项目2到4。但是,项目1的关键是使用signif创建标签,并使用axis创建自定义轴。

首先,一些样本数据跨越几个数量级:

set.seed(14)
x <- 10^(rnorm(30))
y <- 10^(rnorm(30))

接下来,选择要让轴刻度线位于的位置,并设置一个包含标签的向量。

tick_pos <- c(.03, .1, .3, 1, 3, 10, 30, 100, 300)
tick_lbl <- character(length(tick_pos))

然后从刻度位置计算适当的标签:

for (i in seq_along(tick_pos)) {
  tick_lbl[i] <- signif(tick_pos[i], 1)
}

请注意,您不能将整个tick_pos向量传递给signif因为该函数会强制结果使用单一格式。

> signif(tick_pos, 1)
[1] 3e-02 1e-01 3e-01 1e+00 3e+00 1e+01 3e+01 1e+02 3e+02

最后,创建没有轴的图,添加自定义轴,然后添加框架:

plot(x, y, log = 'xy', axes = FALSE)
axis(1, labels = tick_lbl, at = tick_pos)
axis(2, labels = tick_lbl, at = tick_pos)
box()

带有1个sig图刻度标签的图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM