标签: r-grid

尽管线条大小,如何保持线型间距不变

当尺寸不同时,我一直试图在 ggplot2 或网格中绘制线段之间具有相等间距的线。但是我没有成功,所以我向你寻求帮助。

在下面的示例中,如何在线条大小不同的情况下保持线段之间的绝对间距相等?

我想避免使用自定义makeContent.myclass方法来控制自己。

library(ggplot2)
library(grid)

df <- data.frame(
  x = c(1:2, 1:2),
  y = c(1:2, 2:1),
  size = c(1,1,10,10)
)

# In ggplot2
ggplot(df, aes(x, y, size = size, group = size)) +
  geom_line(linetype = 2)
Run Code Online (Sandbox Code Playgroud)

# In grid
lines <- polylineGrob(
  x = scales::rescale(df$x), 
  y = scales::rescale(df$y), 
  id = c(1,1,2,2),
  gp = gpar(lty = 2, lwd = c(1, 10))
)

grid.newpage(); grid.draw(lines)
Run Code Online (Sandbox Code Playgroud)

我想要类似于以下在 illustrator 中制作的东西。请注意,红线段的长度相等。

在此处输入图片说明

有任何想法吗?谢谢阅读!

r ggplot2 r-grid

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

使用TableGrob更改单元格的文本颜色

有没有办法在使用tableGrob和ggplot2时单独更改单元格文本的颜色?

例如,在下面的代码中,如果带有1的单元格可能是蓝色而带有2的单元格可能是红色,而3:8的单元格全黑,则会很棒.

library(ggplot2)
library(grid)

mytable = as.table(matrix(c("1","2","3","4","5","6","7","8"),ncol=2,byrow=TRUE))
mytable = tableGrob(mytable,gpar.coretext = gpar(col = "black", cex = 1))
mydf = data.frame(x = 1:10,y = 1:10)

ggplot( mydf, aes(x, y)) + annotation_custom(mytable)
Run Code Online (Sandbox Code Playgroud)

r colors ggplot2 gridextra r-grid

9
推荐指数
3
解决办法
5744
查看次数

ggplot2:如何获得facet_grid()的labeller = label_both和facet_wrap()的ncol选项的合并功能?

我正在使用ggplot2创建一个boxplot.我能够成功创建boxplot但是在ggplot2中将两个要素与facet合并时遇到问题:

>tmpdf

    value  treat FoldChange Gene Pvalue.Adj
 523.8589 Normal     -1.899    A    0.02828
 489.7638 Normal     -1.899    A    0.02828
 642.0126 Cancer     -1.899    A    0.02828
 928.8136 Cancer     -1.899    A    0.02828
 624.7892 Normal     -1.899    A    0.02828
  53.8685 Normal     -7.135    B    0.00012
 184.6473 Normal     -7.135    B    0.00012
  76.2712 Cancer     -7.135    B    0.00012
  48.0607 Cancer     -7.135    B    0.00012
 177.9528 Normal     -7.135    B    0.00012
4581.2847 Normal     -1.886    C    0.04924
7711.3411 Normal     -1.886    C    0.04924
6007.9852 Cancer     -1.886    C    0.04924
5940.9232 Cancer     -1.886    C    0.04924
4433.0949 Normal     -1.886 …
Run Code Online (Sandbox Code Playgroud)

r facet ggplot2 r-grid

9
推荐指数
1
解决办法
2079
查看次数

使用网格R包添加边距

我不知道如何使用网格R包指定PDF打印的边距.

我创建了一个grid.arrange()对象,我把它放在这样的PDF中:

pdf('test.pdf',11.69,8.27)
grid.arrange(textGrob('text1', gp=gpar(cex=4)),
             ncol=1, main=textGrob('title', gp=gpar(cex=1.5)),
             widths = c(1), heights = c(1))
dev.off()
Run Code Online (Sandbox Code Playgroud)

但标题是在表格的顶部边缘推动.我想补充保证金.如果我为标题添加一个textGrob而不是main=函数,我可以让它远离顶部,但它不是我的解决方案,因为我必须放置图形,它们也接近边缘.

r margins r-grid

9
推荐指数
1
解决办法
1924
查看次数

'ggplot2' 绘图的对数网格

我正在尝试使用 ggplot2 创建一个对数间隔网格的图,如下图所示。我得到等距网格,但没有记录间隔网格。我知道我缺少一些我现在似乎没有得到的参数。我已经看到很多关于这个主题的问题,比如使用 ggplot2 (dynamic not manual) 对数正常刻度的漂亮刻度,但没有解决我正在寻找的问题。

set.seed(5)
x <- rlnorm(1000, meanlog=3.5, sdlog=1)
y <- rlnorm(1000, meanlog=4.0, sdlog=1)
d <- data.frame(x, y)

plot(x, y, log="xy", las=1)
grid(nx=NULL, ny=NULL, col= "blue", lty="dotted", equilogs=FALSE)
library(magicaxis)
magaxis(side=1:2, ratio=0.5, unlog=FALSE, labels=FALSE)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

library(ggplot2)
library(MASS)
library(scales)
a <- ggplot(d, aes(x=x, y=y)) + geom_point() +
     scale_x_log10(limits = c(1, NA), 
                   labels = trans_format("log10", math_format(10^.x)),
                   breaks=trans_breaks("log10", function(x) 10^x, n=4)) +
     scale_y_log10(limits = c(1, NA),
                   labels = trans_format("log10", math_format(10^.x)),
                   breaks=trans_breaks("log10", function(x) 10^x, n=4)) +
     theme_bw() + theme(panel.grid.minor …
Run Code Online (Sandbox Code Playgroud)

r scatter-plot ggplot2 r-grid

9
推荐指数
2
解决办法
1万
查看次数

使用coord_polar()"缩放"/"缩放"

我有一个使用geom_smooth()的极坐标图.平滑的黄土线虽然非常小,但环绕着地块的中心.我想"放大",这样你就可以更好地看到它.

使用类似的东西scale_y_continuous(limits = c(-.05,.7))会使geom_smooth环更大,但它也会改变它,因为它将使用limits = c(-.05,.7)参数限制的数据点重新计算.

对于笛卡尔图,我可以使用类似于coord_cartesian(ylim = c(-.05,.7))剪切图表而不是基础数据的东西.但是我看不到这样做coord_polar()

有任何想法吗?我认为grid.clip()在网格包中可能有一种方法可以做到这一点,但我没有太多运气.

有任何想法吗?

我的情节现在看起来像是什么,注意"更高"的红线: 目前的情节,注意较大的红线,但它的小.

我想画的是什么: 我想得到什么

当我使用scale_y_continuous()音符"更高"的蓝线时,我得到的是,它仍然没那么大. 我用<code> scale_y_continuous()</ code>得到的信息,注意蓝线现在更大,但仍然没有那么大.

r ggplot2 r-grid

9
推荐指数
1
解决办法
648
查看次数

如何创建空间点网格

library(reshape2)
library(data.table)
library(dplyr)
library(magrittr)
library(ggplot2)
library(scales)
library(gstat)
library(DescTools)
library(sp)



#I want a colorado grid#
data("colorado.grid")

#making cordinates into spatial points dataframe#
coordinates(Gold_tracer_kri) <- ~ long_orig + lat_orig
#attempt at kriging but no grid#
lzn.kriged <- krige(Au ~ 1, Gold_tracer_kri, colorado.grid, model=lzn.fit)

lzn.kriged %>% as.data.frame %>%
ggplot(aes(long_orig=long_orig, lat_orig=lat_orig)) +     geom_tile(aes(fill=var1.pred)) + coord_equal() +
scale_fill_gradient(low = "yellow", high="red") +
scale_x_continuous(labels=comma) + scale_y_continuous(labels=comma) +
theme_bw()
Run Code Online (Sandbox Code Playgroud)

加载空间域以进行插值

data("meuse.grid")
Run Code Online (Sandbox Code Playgroud)

我正在尝试在R中使用克里金方法,但由于无法为我的数据找到网格而被卡住了.我的数据占据了美国所有的科罗拉多州,我希望能够获得一个网格来中断我的数据.与我正在关注的示例中使用的meuse.grid类似.

任何帮助,将不胜感激

r geospatial r-grid desctools

9
推荐指数
1
解决办法
1万
查看次数

如何改变grid.draw的位置

我正在安排ggplot2生成的图.我必须使用print打印出plot和grid.draw来显示图例.

示例代码:

p0 <- ggplot(data = iris, geom = 'blank',
         aes(y = Petal.Width, x = Petal.Length, color = Species)) + geom_point() +
      theme(axis.title.x = element_blank(),
            axis.title.y = element_blank(),
            legend.position = "none")

p1 <- ggplot(data = iris, geom = 'blank',
         aes(y = Petal.Length, x = Petal.Width, color = Species)) + geom_point() +
       theme(axis.title.x = element_blank(),
             axis.title.y = element_blank(),
             legend.position = "none")

g_legend <- function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)
}

p <- …
Run Code Online (Sandbox Code Playgroud)

r r-grid

8
推荐指数
2
解决办法
6128
查看次数

不再支持ggplot2中的箭头()

在新机器和ggplot2上安装了R 2.15.2.最新版本的R不再支持网格包,但ggplot2使用网格作为arrow()函数,如这些示例中所示.

http://docs.ggplot2.org/current/geom_segment.html

一直无法找到解决方法.有什么建议?

r ggplot2 r-grid

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

R中的牛皮网格的一个共享传奇

我刚刚用包构建了一个网格cowplot(用来标记AD的图).这些地块是用包装制成的ggplot2:

pfour<-ggplot(four, aes(x=Concentration, y=Percentage, fill=Phenotype)) + 
 geom_bar(stat='identity',color='black') +
 scale_fill_grey(start = .4, end = .9) + 
 theme_bw()+ylab("Distribution") + 
 xlab("Contentration [mg/ml]") + 
 ggtitle("96 hpf") +
 theme(legend.title = element_text(colour="black", size=10, face="bold")) +
 theme(legend.background = element_rect(fill="white",
                                        size=0.5, linetype="solid", 
                                        colour ="black")) +
 scale_x_discrete(limits=c('uninjected','control','0.002', '0.02', '0.2'),
                  labels=c('uninjected\n(n=251)',
                           'control\n(n=248)', 
                           '0.002\n(n=205)', 
                           '0.02\n(n=222)', 
                           '0.2\n(n=203)'))
Run Code Online (Sandbox Code Playgroud)

数据看起来像那样(4个不同的表,百分比略有不同但原理相同):

Concentration,Percentage,Phenotype
uninjected,0.996015936,0
uninjected,0,1
uninjected,0.003984064,2
uninjected,0,3
uninjected,0,4
control,0.995967742,0
control,0.004032258,1
control,0,2
control,0,3
control,0,4
0.002,0.985365854,0
0.002,0.004878049,1
0.002,0.004878049,2
0.002,0,3
0.002,0.004878049,4
0.02,0.981981982,0
0.02,0.004504505,1
0.02,0.004504505,2
0.02,0.004504505,3
0.02,0.004504505,4
0.2,0.985221675,0
0.2,0.004926108,1
0.2,0,2
Run Code Online (Sandbox Code Playgroud)

它看起来像那样:

情节

代码是:

plot_grid(ponezoom, ptwozoom,pthreezoom,pfourzoom, align='h', labels=c('A', …
Run Code Online (Sandbox Code Playgroud)

r legend r-grid cowplot

8
推荐指数
2
解决办法
6688
查看次数

标签 统计

r ×10

r-grid ×10

ggplot2 ×6

colors ×1

cowplot ×1

desctools ×1

facet ×1

geospatial ×1

gridextra ×1

legend ×1

margins ×1

scatter-plot ×1