相关疑难解决方法(0)

在ggplot2生成的图表下方显示文本

我试图在ggplot2中创建的图表下面显示一些有关数据的信息.我想使用图的X轴坐标绘制N变量,但Y坐标需要距离屏幕底部10%.实际上,期望的Y坐标已经作为y_pos变量存在于数据框中.

我可以想到使用ggplot2的3种方法:

1)在实际绘图下方创建一个空图,使用相同的比例,然后使用geom_text在空白图上绘制数据.这种方法有点有效,但非常复杂.

2)geom_text用于绘制数据,但以某种方式使用y坐标作为屏幕的百分比(10%).这将强制数字显示在图表下方.我无法弄清楚正确的语法.

3)使用grid.text显示文本.我可以轻松地将它设置在屏幕底部的10%,但我无法确定如何设置X coordindate以匹配绘图.我试图使用grconvert捕获最初的X位置但是也无法使其工作.

以下是虚拟数据的基本情节:

graphics.off()      # close graphics windows   

library(car)
library(ggplot2)  #load ggplot
library(gridExtra) #load Grid
library(RGraphics) # support of the "R graphics" book, on CRAN

#create dummy data
test= data.frame(
  Group = c("A", "B", "A","B", "A", "B"), 
  x = c(1 ,1,2,2,3,3 ),
  y = c(33,25,27,36,43,25),
  n=c(71,55,65,58,65,58),
  y_pos=c(9,6,9,6,9,6)
  )

#create ggplot
p1 <- qplot(x, y, data=test, colour=Group) +
  ylab("Mean change from baseline") + 
  geom_line()+
  scale_x_continuous("Weeks", breaks=seq(-1,3, by = 1) …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

35
推荐指数
4
解决办法
4万
查看次数

ggplot2:将样本大小信息添加到x轴刻度标签

此问题与 创建自定义geom以计算汇总统计信息并在*绘图区域外显示* (注意:所有函数都已简化;没有错误检查正确的对象类型,NA等)

在基础R中,很容易创建一个生成条带图的函数,其中样本大小在分组变量的每个级别下面指示:您可以使用以下mtext()函数添加样本大小信息:

stripchart_w_n_ver1 <- function(data, x.var, y.var) {
    x <- factor(data[, x.var])
    y <- data[, y.var]
# Need to call plot.default() instead of plot because 
# plot() produces boxplots when x is a factor.
    plot.default(x, y, xaxt = "n",  xlab = x.var, ylab = y.var)
    levels.x <- levels(x)
    x.ticks <- 1:length(levels(x))
    axis(1, at = x.ticks, labels = levels.x)
    n <- sapply(split(y, x), length)
    mtext(paste0("N=", n), side = 1, line = 2, at = x.ticks)
}

stripchart_w_n_ver1(mtcars, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 ggproto

15
推荐指数
3
解决办法
8781
查看次数

如何在ggplot2中的x轴下添加注释?

我有以下图表:

library(ggplot2)
library(scales)
library(magrittr)
df1 <-
  structure(
    list(
      x = structure(
        1:5, .Label = c("5", "4", "3", "2",
                        "1"), class = "factor"
      ), y = c(
        0.166666666666667, 0.361111111111111,
        0.0833333333333333, 0.222222222222222, 0.291666666666667
      )
    ), .Names = c("x",
                  "y"), row.names = c(NA,-5L), class = c("tbl_df", "tbl", "data.frame"), drop = TRUE
  )

df1 %>% ggplot(aes(x , y )) + geom_bar(stat = "identity") +
  scale_y_continuous(labels = percent) 
Run Code Online (Sandbox Code Playgroud)

我想在5和1之下添加带有粗体文本的两行注释.例如,'最高\nvalue'低于5,'最低\n值'低于1.

我试过geom_text但我不能把文字放在我想要的地方.

r ggplot2

10
推荐指数
2
解决办法
9983
查看次数

标签 统计

ggplot2 ×3

r ×3

ggproto ×1