簡體   English   中英

R 中地塊標簽大小的問題

[英]Problems with the size of the labels of the plots in R

我對地塊標簽的大小有疑問。 所以,這是代碼:

column1 <- c(0.18936045, 0.55010315,  0.23474801, 0.02578839)
column2 <- c(0.20522653, 0.51235168, 0.26060781, 0.02181398 )

example_data <- 
  data.frame(
    rowNames = c('[-2.34898,-0.83219]', '(-0.83219,0.684599]', '(0.684599,2.20139]', '(2.20139,3.71818]'),
    column1 = column1,
    column2 = column2
  )

plot(data = example_data, column1 ~ column2, xlab = "Marginal probs scaledsci",
     ylab = "Actual probs from data", pch = 20, col = 'blue')
text(data = example_data, column1 ~ column2, labels = rowNames, cex = .6, pos = 2.99, col = 'red')

這是我獲得的plot:獲得的plot

所以,我想讓所有帶有標簽的點都可見。 那么,有人可以幫我解決這個問題嗎?

如果您想保持您的文本 position 相對於您使用pos參數定義它的方式的點,一種選擇是增加 x 軸范圍的限制(特別是在左側),例如:

xlim2 <- {r2=diff(range(column1))*.6; c(mean(column1)-r2, max(column1))}

plot(data = example_data, column1 ~ column2, xlab = "Marginal probs scaledsci",
     ylab = "Actual probs from data", pch = 20, col = 'blue', xlim=xlim2)
text(data = example_data, column1 ~ column2, labels = rowNames, cex = .6, pos = 2.99, col = 'red')

對於情節,我更喜歡ggplot 當我遇到標簽位置問題(與數據點重疊等)時,我經常添加 package ggrepel的功能。 下面的例子。

library(ggplot2)
library(ggrepel)
ggplot(example_data, aes(x = column1, y = column2)) + geom_point(size = 4, col = "blue") + labs(x = "Marginal probs scaledsci", y = "Actual probs from data") + geom_text_repel(label = example_data$rowNames, col = "red", nudge_y = 0.02) +
theme_bw() +  theme(text = element_text(size = 15))

nudge_y參數以 0.02 個單位的 y 變量移動 label。 它是可選的,但在我看來更好看。

陰謀

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM