相关疑难解决方法(0)

ggplot2 - 在剧情之外注释

我想将样本大小值与绘图上的点相关联.我可以geom_text用来定位点附近的数字,但这很麻烦.将它们沿着图的外边缘排列会更加清晰.

例如,我有:

df=data.frame(y=c("cat1","cat2","cat3"),x=c(12,10,14),n=c(5,15,20))

ggplot(df,aes(x=x,y=y,label=n))+geom_point()+geom_text(size=8,hjust=-0.5)
Run Code Online (Sandbox Code Playgroud)

这产生了这个情节: 在此输入图像描述

我更喜欢这样的东西: 在此输入图像描述

我知道我可以创建第二个图并使用grid.arrange(这篇文章),但确定textGrobs与y轴对齐的间距会很繁琐.有更简单的方法吗?谢谢!

annotations r ggplot2

62
推荐指数
3
解决办法
6万
查看次数

在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 boxplot

我可以用ggplot2制作这样的情节吗?

# data
require(vegan)
data(dune)
data(dune.env)

# RDA
RDA <- rda(dune ~ A1, data = dune.env)

# extract species scores
df <- data.frame(spec_scores = scores(RDA)$species[ , 1], 
                 taxa = rownames(scores(RDA)$species))
df <- df[abs(df$spec_scores) > 0.05, ]

# plot
par(mar = c(5,4,4,8))
# boxplot of sites-scores along A1-axis
boxplot(scores(RDA)$sites[ , 1] ~ dune.env$Management)
abline(h = 0, lty = "dotted")

# add species scores to plot
rug(df$spec_scores, side=4)
linestack(df$spec_scores, labels=df$taxa, at = par("usr")[2], add = TRUE, hoff = 1)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

基本上我正在寻找一种方法如何在箱线图下方绘制标记的地毯.

任何提示或建议?

r ggplot2 vegan

16
推荐指数
1
解决办法
1337
查看次数

在ggplot2中使用grconvertX/grconvertY

我想弄清楚如何在ggplot中使用grconvertX/grconvertX.我的最终目标是使用和从用户坐标到设备坐标向ggplot2图形(可能是晶格)添加注释.我知道它可以用grobs完成,但我想知道是否有更简单的方法.grid.textgrid.lines

以下代码允许我将值从用户坐标传递到ndc坐标,并使用这些值来注释绘图grid.text.

graphics.off()      # close graphics windows   

library(grid)
library(gridBase)


test= data.frame(
  x = c(1,2,3),
  y = c(12,10,3),
  n = c(75,76,73)
  )

par(mar = c(13,5,2,3))

plot(test$y ~ test$x,type="b", ann=F)

for (i in 1:nrow(test))

{
  X=grconvertX(i , from="user", to="ndc")
  grid.text(x=X, y =0.2, label=paste("GRID.text at\nuser.x=", i, "\n", "ndc.x=", (signif( X, 5))   ) ) 
  grid.lines(x=c(X, X), y = c(0.28, 0.33) )
}
#add some code to save as PDF ...
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

该代码基于我以前的一篇文章的解决方案:混合X和Y坐标系.您可以看到原始图中的x坐标是如何转换为ndc的.这种方法的优点是我可以使用Y的设备坐标.

我假设我可以轻松地在ggplot2(也可能在 …

r ggplot2 lattice

8
推荐指数
1
解决办法
1120
查看次数

如何在ggplot2绘图区域获得多种背景颜色?

ggplot2图表上的绘图区域(但不是面板区域)可以有多种背景颜色吗?预感告诉我,有可能将轴作为某种背景颜色.

这就是我目前的图表:

这就是我想要实现的目标:

[最终的颜色肯定会有所不同.我只是以最简单的形式使用这个例子以便于讨论]

我尝试将"填充"参数传递给:

theme(axis.text.y = element_text(fill = "red"),
Run Code Online (Sandbox Code Playgroud)

但它显然失败了,因为这个论点是为了element_rect.

r ggplot2

8
推荐指数
1
解决办法
412
查看次数

避免在ggplot中沿轴剪切点

我正在绘制一个时间序列,其响应变量的平均值为月(x轴)的点(y轴).

位于x轴上的值(即0值)被剪裁.我可以改变y轴的极限以包括一些低于0的填充,但我不愿意.

有没有办法在x轴的前面或顶部绘制这些0点?

r clipping ggplot2

7
推荐指数
2
解决办法
5287
查看次数

ggplot2:在绘图区域外绘制 geom_segment()

我在 ggplot2 中创建了两个方面图。我想在绘图区域外添加一个箭头。多个问题试图解决这个问题: How to draw lines out of plot area in ggplot2? 在 ggplot2 生成的图下方显示文本

但我不能让我的例子工作。另外,我希望有一种更简单的方法来实现这一点?

我试图增加plot.margins和使用coord_cartesian(),但都没有帮助。

在此处输入图片说明

相反,我想要:

在此处输入图片说明

我的虚拟示例:

# read library to assess free data
library(reshape2)
library(ggplot2)


ggplot(tips,
       aes(x=total_bill,
           y=tip/total_bill)) + 
  geom_point(shape=1) +
  facet_grid(. ~ sex) +
  # define the segment outside the plot
  geom_segment(aes(x = 10, 
                   y = -0.25, 
                   xend = 10, 
                   yend = 0),
               col = "red",
               arrow = arrow(length = unit(0.3, "cm"))) +
  theme_bw() +
  # limit the displayed plot extent …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

6
推荐指数
1
解决办法
3889
查看次数

将彩色箭头添加到ggplot2的轴(部分在绘图区域外)

我想添加一个彩色箭头(轴的全长)来显示向一个方向移动的时间(这可以假设,但是对于这个图,没有数值,所以我希望箭头显示方向).我可以geom_segment用来绘制它,但是缺少了绘图区域之外的部分.

我看过这篇文章:R&ggplot2:如何在轴标签下获得箭头?但这个解决方案是轴标题的黑客.这篇文章:https://stackoverflow.com/a/10542622/1000343显示文本区域之外的行,但不是彩色箭头.

MWE

library(ggplot2); library(grid); library(scales)

dat <- data.frame(Time=0:5, y=0:5)

ggplot(dat, aes(x=Time, y=y)) +
    geom_area(alpha=.1) + theme_bw() +
    scale_y_continuous(expand = c(0, 0)) +
    scale_x_continuous(expand = c(0, 0)) +
    theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
          axis.text.x=element_blank(),
          axis.ticks.x=element_blank()
    )    
Run Code Online (Sandbox Code Playgroud)

我试过了:

ggplot(dat, aes(x=Time, y=y)) +
    geom_area(alpha=.1) + theme_bw() +
    scale_y_continuous(expand = c(0, 0)) +
    scale_x_continuous(expand = c(0, 0)) +
    theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
          axis.text.x=element_blank(),
          axis.ticks.x=element_blank()
    ) +
    geom_segment(aes(x=0, xend = 5 , y=0, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

5
推荐指数
2
解决办法
3237
查看次数

应用于带有facet_wrap的箱形图时,删除一个tableGrob

我正在使用下面的代码来丰富一个箱形图,其中包含在x轴上创建的分类变量的汇总表.

# Libs
require(ggplot2); require(gridExtra); require(grid); require(ggthemes)

# Data
data(mtcars)

# Function to summarise the data
fun_dta_sum <- function(var_sum, group, data) {
    sum_dta <- data.frame(
        aggregate(var_sum ~ group, FUN = min, data = data),
        aggregate(var_sum ~ group, FUN = max, data = data),
        aggregate(var_sum ~ group, FUN = mean, data = data))

    sum_dta <- sum_dta[,c(1,2,4,6)]
    colnames(sum_dta) <- c("Group (x axis)", "min", "max", "mean")
    rownames(sum_dta) <- NULL
    sum_dta[,-1] <-round(sum_dta[,-1],1)
    return(sum_dta)

}

# Graph
ggplot(data = mtcars, aes(x = cyl, y = …
Run Code Online (Sandbox Code Playgroud)

charts r ggplot2 boxplot gridextra

5
推荐指数
1
解决办法
1033
查看次数

在ggplot2中的图例上的某些级别之间添加线条

我有一个这样的情节:

我的示例情节

这是一个马赛克图,其中一些组上方有一条黑线.我希望黑线也能成为传奇.在这个例子中,图例有6个级别,高于2级和4级的正方形,我想要一条黑线.

我尝试了类似的方法: 如何在ggplot2中绘制绘图区域以外的线条,但不幸的是,当我调整绘图大小时,线条会随着情节移动而不是传说,它们会在错误的位置移动.

以下是制作上图的示例代码.

exampledata<-data.frame(var1Center=c(rep(.2, 6) ,rep(.5,6) ,rep(.8,6)), 
                        var2Height=c(.2,.2,.2,.1,.1,.2, .1,.1,.05,.45,.1,.2,  .4,.07,.03,.1,.35,.05),
                        var1=c(rep("Thing 1", 6), rep("Thing 2", 6), rep("Thing 3", 6)),
                        var2=c( rep(c("A", "B", "C","D", "E", "F"), 3)), 
                        marginVar1=c(rep(.4,6) ,rep(.2,6), rep(.4,6)))

plotlines<-data.frame(xstart=c(0, 0,.4, .4, .6,.6), xstop=c(.4,.4, .6,.6, 1,1), value=c(.4, .7, .2,.7, .47, .6))

ggplot(exampledata, aes(var1Center, var2Height)) +
  geom_bar(stat = "identity", aes(width = marginVar1, fill = var2)) +
  scale_x_continuous(breaks=exampledata$var1Center, labels=exampledata$var1, expand=c(0,0))+
  theme_bw()+scale_y_continuous(name="Proportion",expand=c(0,0))+
  guides(fill = guide_legend(reverse=TRUE))+
  theme(panel.border=element_blank(), panel.grid=element_blank())+
  theme(axis.text.x=element_text(angle=90, hjust=1, vjust=.3))+
  geom_segment(data=plotlines, aes(x=xstart, xend=xstop, y=value, yend=value))
Run Code Online (Sandbox Code Playgroud)

r legend ggplot2

5
推荐指数
1
解决办法
183
查看次数

标签 统计

ggplot2 ×10

r ×10

annotations ×1

boxplot ×1

charts ×1

clipping ×1

gridextra ×1

lattice ×1

legend ×1

vegan ×1