【问题标题】:How to write after decimal zero in ggplot (geom_text)如何在ggplot(geom_text)中写小数点零后
【发布时间】:2014-08-04 05:38:07
【问题描述】:

我想知道如何在多面 ggplot 图中添加小数点零后。下图更清楚。我想使用geom_textggplot 的两个facets 中写入r-squared =0.61 和0.30。它正确写入 0.61,但在第二个图中仅写入 0.3 而不是 0.30。看图。 我的工作数据和代码如下。

 dput(ssdata)
 structure(list(Value = c(0.0776799352545487, 0.0249900650410425, 
 0.0530261633888124, 0.0567436050950435, 0.0120449632406235, 0.0148445528174501, 
 0.00322103330067226, 0.0841269995036878, 0.0667567417539399, 
 0.0353554071120496, 0.0168391247006024, 0.0187554325376238, 0.0862325738977503, 
 0.0353403282165527, 0.0459572764543387, 0.0153406669137266, 0.0390336212195695, 
 0.0620484352111816, 0.0489626884460449, 0.0221186299849756), 
 Ft = c(1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 
 1, 1, 1), RT = c(869, 722, 1790, 875, 1524, 1314, 1061, 919, 
 1525, 1127, 932, 1182, 1046, 1004, 1238, 1497, 1066, 1500, 
 1654, 903), Season = c("Winter", "Spring", "Spring", "Winter", 
 "Summer", "Fall", "Fall", "Winter", "Winter", "Spring", "Summer", 
 "Summer", "Winter", "Winter", "Spring", "Fall", "Spring", 
 "Winter", "Spring", "Spring"), fac = structure(c(1L, 2L, 
 2L, 1L, 3L, 4L, 4L, 1L, 1L, 2L, 3L, 3L, 1L, 1L, 2L, 4L, 2L, 
 1L, 2L, 2L), .Label = c("Winter", "Spring", "Summer", "Fall"
 ), class = "factor")), .Names = c("Value", "Ft", "RT", "Season", 
 "fac"), row.names = c(51L, 1320L, 1341L, 427L, 2384L, 3342L, 
  3111L, 16L, 330L, 1062L, 2501L, 2500L, 61L, 711L, 941L, 3171L, 
 973L, 610L, 1418L, 1524L), class = "data.frame")

 dput(data)
 structure(list(fac = structure(1:4, .Label = c("Winter", "Spring", 
 "Summer", "Fall"), class = "factor"), tp = c("R^2== 0.26", "R^2== 0.61", 
 "R^2== 0.30", "R^2== 0.22"), ttp = c("n== 844", "n== 844", "n== 844", 
  "n== 844"), family = c("serif", "serif", "serif", "serif"), fontface = c("italic", 
  "italic", "italic", "italic"), size = c(5, 5, 5, 5), x = c(0, 
  0, 0.8, 0.8), y = c(2000, 2000, 2000, 2000), yy = c(1800, 1800, 
  1800, 1800)), .Names = c("fac", "tp", "ttp", "family", "fontface", 
  "size", "x", "y", "yy"), row.names = c(NA, -4L), class = "data.frame")

代码:

plot.sea1<-ggplot(ssdata,aes(x=10*Value, y= RT))+
geom_point()+
#stat_smooth(method="loess", se=F,fullrange=TRUE, size=0.8)+
facet_grid(fac~.,scales="free_x")+
geom_text(data=data,aes(x=x,y=y,label=tp),parse=TRUE,inherit.aes=F,show_guide=  F,size=3)
plot.sea1

【问题讨论】:

  • 当您生成变量 tp 时,您是否正在格式化它 - 即 formatC(0.3, format='f', digits=2) 保留尾随零
  • @user20650 我用 sprintf 函数生成 tp 为 sprintf("%3.2f", r2val[1,1]) 其中 r2val 是 r sqr 的结果的数据框。
  • 嗯,我认为这会奏效 - 你能分享更多你的代码或一个小的工作示例吗,谢谢
  • 回复:您的添加代码r2valsea.data 是在哪里定义的?
  • 好的,我认为您正在将完整的 R2 值作为字符串传递正确 [像 eq &lt;- data.frame(fac = 0:1, r2 = c("R^2== 0.61" , "R^2== 0.30")) 这样的数据。解析时遇到问题

标签: r ggplot2 decimal facet geom-text


【解决方案1】:

由于 r2 字符串的解析方式,您遇到了问题。

编辑

对您的数据使用较早帖子(如下)中的方法:

lm_eqn <- function(ssdata){
           m = lm(RT ~ Value, ssdata)
           eq <- substitute(italic(r)^2~"="~r2, 
                   list(r2 = formatC(summary(m)$r.squared, digits = 2)))
           as.character(as.expression(eq))          
        }

eq1 <- ddply(ssdata,.(fac), lm_eqn)

plot.sea1 <- ggplot(ssdata,aes(x=10*Value, y= RT)) +
                 geom_point() +
                 stat_smooth(method="loess", se=F,fullrange=TRUE, size=0.8) +
                 facet_grid(fac~.,scales="free_x") + 
                 geom_text(data=eq1 ,aes(x = 0.6, y = 1200, label=V1), 
                                                               parse = TRUE)

第一次发帖

# example using mtcars
data(mtcars)

# facetted plot similar to yours
p <- ggplot(mtcars, aes(mpg, wt)) + 
        geom_point(aes(color=factor(vs))) +
        stat_smooth(method="loess", se=F,fullrange=TRUE, size=0.8) + 
        facet_grid(am ~.)


eq <- data.frame(am = c(0,1), r2 =  c("italic(r)^2 == 0.30",
               as.character(as.expression(substitute(italic(r)^2~"="~r2, 
                       list(r2 = formatC(0.30, format = "f", digits = 2)))))))

eq
#  am                         r2
# 1  0        italic(r)^2 == 0.30
# 2  1 italic(r)^2 ~ "=" ~ "0.30"

# add the text
p + geom_text(data=eq ,aes(x = 25, y = 5, label=r2), parse=TRUE)

只有第二个被正确解析。当你只传递字符串时,训练零被删除。

因此,使用 JT85 答案 here 提供了另一种生成 r2 字符串的方法。

# define a function to generate r2 string
lm_eqn <- function(mtcars) {
              m = lm(mpg ~ wt, mtcars)
              eq <- substitute(italic(r)^2~"="~r2, 
                          list(r2 = formatC(summary(m)$r.squared, digits = 2)))
                                               as.character(as.expression(eq))   
          }

library(plyr)
# generate r2 string
eq1 <- ddply(mtcars, .(am), lm_eqn)

# plot
p + geom_text(data=eq1 aes(x = 25, y = 5, label=V1), parse = TRUE)

【讨论】:

  • @EricFail;干杯,但完全取自@JT85 在链接上的回答
猜你喜欢
  • 2014-09-23
  • 1970-01-01
  • 1970-01-01
  • 2015-05-25
  • 2011-10-12
  • 1970-01-01
  • 1970-01-01
  • 2017-09-22
  • 1970-01-01
相关资源
最近更新 更多