小编Z.L*_*Lin的帖子

R 中的 ggplot2:在绘图和下划线文本之外进行注释

几个小时以来,我一直在挠头。我现在所拥有的:

library(ggplot2)
library(grid)

all_data = data.frame(country=rep(c("A","B","C","D"),times=1,each=20),
                  value=rep(c(10,20,30,40),times=1,each=20),
                  year = rep(seq(1991,2010),4))


# PLOT GRAPH

p1 <- ggplot() + theme_bw() + geom_line(aes(y = value, x = year, 
colour=country), size=2,
                                    data = all_data, stat="identity") + 
theme(plot.title = element_text(size=18,hjust = -0.037), legend.position="bottom", 
    legend.direction="horizontal", legend.background = element_rect(size=0.5, linetype="solid", colour ="black"),
    legend.text = element_text(size=16,face = "plain"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
    panel.border = element_blank(),axis.line = element_line(colour = "black"),legend.title = element_blank(),
    axis.text=element_text(size=18,face = "plain"),axis.title.x=element_text(size=18,face = "plain", hjust = 1,
                                                                             margin = margin(t = 10, r = …
Run Code Online (Sandbox Code Playgroud)

r annotate ggplot2 geom-text

3
推荐指数
1
解决办法
5072
查看次数

错误 - 使用 ggrepel 包在 ggplot2 中可视化数据

我尝试在 ggplot2 库中添加 ggrepel 来绘制图表:

set.seed(42)
ggplot(mtcars) +
  geom_point(aes(wt, mpg), size = 5, color = 'grey') +
  geom_label_repel(aes(wt, mpg, fill = factor(cyl), label = rownames(mtcars)),
                   fontface = 'bold', color = 'white',
                   box.padding = 0.35, point.padding = 0.5, 
                   segment.color = 'grey50') + 
  theme_classic(base_size = 16)
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

Error in convertUnit(x, unitTo, "x", "dimension", "x", "dimension", valueOnly = valueOnly) : 
  'x' argument must be a unit object
Run Code Online (Sandbox Code Playgroud)

谢谢?

r graph ggplot2 ggrepel

3
推荐指数
1
解决办法
753
查看次数

调整 R 中帕累托图上的第二个 y 轴

我在 R 中创建了两个帕累托图表,都使用相同的数据。一个使用 ggplots stat_pareto,另一个使用 qcc 库中的 pareto.chart 函数。

ggplot(DT4, aes(x = reorder(sap_object_type_desc, -sum_duration), y = 
  sum_duration)) + 
  geom_bar(stat="identity") +
  theme(axis.text.x=element_text(angle=90,hjust=1)) + 
  stat_pareto(point.color = "red",   
              point.size = 2,        
              line.color = "black",  
              #size.line = 1,        
              bars.fill = c("blue", "orange"))
Run Code Online (Sandbox Code Playgroud)

ggplot 情节 stat_pareto

或者使用pareto.chart函数

pareto.chart(avector, 
             ylab = "Sum", 
             # xlab = "Objective Type Description", 
             main = "Avector Pareto Chart",
             cumperc = c(20,40,60,80,100)) # or = seq(0, 100, by =25)
Run Code Online (Sandbox Code Playgroud)

pareto_chart 函数

我想要做的是调整上面两个图上的第二个 y 轴,使 100% 累积百分比与最高条对齐,就像第三个例子一样。有什么建议?

示例图

r ggplot2 pareto-chart

3
推荐指数
1
解决办法
1171
查看次数

调整 geom_point 大小,以便绘制较大的值,但在 ggplot2 中不会显得更大?

我遇到的问题是,我希望高于某个阈值 (=<25) 的点不会产生大于设定比例的点。这些较大的点仍然需要显示,并且不能被排除:

d=data.frame(y=c(1,2,6,4,4,6,7,8),
             x=c(8,4,7,5,4,9,2,3),
             coverage=c(0,6,9,88,25,22,17,100),
             col=c(0,.25,.50,.76,.80,1.00,.11,.34)
             )
ggplot() + 
  scale_size(range = c(0, 13),
             breaks = c(0, 5, 10, 20, 25),
             labels = c("0", "5", "10", "20", "25+"),
             guide = "legend"
  ) +
  geom_point(data = d, mapping = aes(x = x, y = y, color = col, size = coverage)) +
  labs(title = "geom_point")
Run Code Online (Sandbox Code Playgroud)

在上面的示例代码中,我有两个点的“覆盖范围”大于 25+ 并且超出了范围。我希望这些点的大小与 25+ 阈值的大小相同。

示例图输出

plot r ggplot2

3
推荐指数
1
解决办法
3240
查看次数

如何更改ggplot2 R中stat_poly_eq的字体大小

我有一个 ggplot 存储在 say 中p,希望添加一个poly_stat_eq图层来显示曲线方程。我想更改文本的字体大小,但找不到有关如何实现它的文档

p + 
  stat_poly_eq(formula = y ~ x, 
               aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
               label.x = 4, label.y = -5, parse = TRUE) 
Run Code Online (Sandbox Code Playgroud)

size fonts r ggplot2

3
推荐指数
1
解决办法
5619
查看次数

ggplot2:x 轴刻度之间的条形

我需要可视化的数据包含 4 个车站之间的火车上的乘客负载。数据以有序的方式提供,即。A站和V站之间的火车上有432名乘客,V站和B站之间有543名乘客,依此类推。在编码示例中,我使用 ggplot2 绘制了数据。

library(tidyverse)

df <- tribble(~Station, ~Passengers,
              "A", 432,
              "V", 543,
              "B", 435,
              "Q", 0)

df$Station <- factor(df$Station, levels=unique(df$Station))

ggplot(data = df, aes(x = Station, y = Passengers)) + 
  geom_bar(stat = "identity")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

问题:我想将 x 轴刻度和车站名称放置在条形之间。目标是将条形向右移动 50%。

r axes ggplot2

3
推荐指数
1
解决办法
820
查看次数

坐标轴的 ggplot `expand_scale()` - 不一致


示例 1

library(tidyverse)
ggplot(mtcars) +
  geom_bar(aes(x = factor(cyl))) +
  scale_y_continuous(expand = expand_scale(mult = c(0, 0)))
Run Code Online (Sandbox Code Playgroud)

1

我的问题似乎是 ggplotexpand_scale()的行为不一致。但这种说法可能是不正确的。让我们从上面的图作为我们的基线开始并深入研究。


示例 2

如果我正确理解了这个论点mult = c(X, Y)我就可以将 ggplot 扩展到图下方的 X% 和图上方的 Y%。这就是我用下面的代码得到的。

ggplot(mtcars) +
  geom_bar(aes(x = factor(cyl))) +
  scale_y_continuous(expand = expand_scale(mult = c(1, 0)))
Run Code Online (Sandbox Code Playgroud)

2


示例 3

ggplot(mpg %>% filter(displ > 6, displ < 8), aes(displ, cty)) + 
  geom_point() + 
  facet_grid(vars(drv), vars(cyl)) + 
  geom_text(aes(label = trans)) +
  scale_x_continuous(expand = c(0, 0)) +
  coord_cartesian(clip = "off")
Run Code Online (Sandbox Code Playgroud)

3

这是我想为示例三和示例四解决的下一个基线。


示例 4

ggplot(mpg %>% …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

3
推荐指数
1
解决办法
1028
查看次数

R:使用 ggplot2 对地图进行分面

我正在尝试使用 ggplot2 对地图进行分面,因为它比 tmap 包更快。遗憾的是我得到的地图并不是我想要的。

library(ggplot2)
library(raster)
library(ggspatial)


chile  <- getData("GADM",country="Chile",level=1)

chile2= chile[c(2,4:5,7,8,12:16),]
chile2$grupo=1
chile3= chile[c(1,3,6,9:11),]
chile3$grupo=2
mapa=rbind(chile2, chile3)

ggplot() +
  layer_spatial(mapa) +
  lims(x = c( -77.1,-65), y = c(-57, -15))+
  facet_wrap(~grupo)
Run Code Online (Sandbox Code Playgroud)

ggplot 地图

遗憾的是上面的地图不是我需要的。通过 tmap,我得到了下面的地图,这正是我真正需要的地图:

TM 地图

你知道如何使用ggplot2解决这个问题吗?

google-maps r facet ggplot2

3
推荐指数
1
解决办法
704
查看次数

将 ggplot 保存在列表中给出了相同的图表

我正在尝试在 3 x 4 网格上绘制 12 个不同的图。但是,它只绘制了最后一个 12 次。谁能帮我?我实在受够了。谢谢

library(ggplot2)
library(gridExtra)

pmax=0.85
K_min = 0.0017
T = seq(100,1200,by=100)  ## ISIs
lambda =1/T
p=list()


for(i in (1:length(lambda))){
p[[i]]<-ggplot(data.frame(x = c(0, 1)), aes(x = x)) +
stat_function(fun = function (x) (lambda[i]*(1-(1-pmax))/K_min)*(1-x)^((lambda[i]/K_min)-1)*
                                    (1-(1-pmax)*x)^-((lambda[i]/K_min)+1),colour = "dodgerblue3")+
scale_x_continuous(name = "Probability") +
scale_y_continuous(name = "Frequency") + theme_bw()
main <- grid.arrange(grobs=p,ncol=4)

}
Run Code Online (Sandbox Code Playgroud)

这段代码生成了正确的图片,但我需要使用 ggplot,因为我的其他数字都在 ggplot 中。

par( mfrow = c( 3, 4 ) )
for (i in (1:length(lambda))){

  f <- function (x) ((lambda[i]*(1-(1-pmax))/K_min)*(1-x)^((lambda[i]/K_min)-1)*
                  (1-(1-pmax)*x)^-((lambda[i]/K_min)+1) )
  curve(f,from=0, to=1, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

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

lubridate - 从日期中提取两位数的日期

我需要从日期中提取两位数的天数。

例如:

date <- as.Date(01-01-2016)
Run Code Online (Sandbox Code Playgroud)
  • year(date) 会给 '2016'
  • month(date) 会产生“1”
  • day(date) 会返回“1”

我想要month()day()返回 '​​01'

标准的 %D 或 %M 在这里似乎不起作用。任何建议,将不胜感激!

r lubridate tidyr

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

标签 统计

r ×10

ggplot2 ×9

annotate ×1

axes ×1

facet ×1

fonts ×1

geom-text ×1

ggrepel ×1

google-maps ×1

graph ×1

lubridate ×1

pareto-chart ×1

plot ×1

size ×1

tidyr ×1