Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggmap上的ggplot图形:标签会破坏小图形_R_Ggplot2_Ggmap - Fatal编程技术网

R ggmap上的ggplot图形:标签会破坏小图形

R ggmap上的ggplot图形:标签会破坏小图形,r,ggplot2,ggmap,R,Ggplot2,Ggmap,我希望在其上生成带有标签的小图表的ggmap,代码如下: p <- get_googlemap( "Poland", maptype = "roadmap", zoom = 6, color = "bw", crop = T, style = 'feature:all|element:labels|visibility:off' #'feature:administrative.country|element:labels|visibi

我希望在其上生成带有标签的小图表的ggmap,代码如下:

p <-
  get_googlemap(
    "Poland",
    maptype = "roadmap",
    zoom = 6,
    color = "bw",
    crop = T,
    style = 'feature:all|element:labels|visibility:off'  #'feature:administrative.country|element:labels|visibility:off' or 'feature:all|element:labels|visibility:off'
  ) %>% 
  ggmap() + coord_cartesian() +
  scale_x_continuous(limits = c(14, 24.3), expand = c(0, 0)) +
  scale_y_continuous(limits = c(48.8, 55.5), expand = c(0, 0))
最后一行编组_by对于生成将粘贴到地图中的图是必不可少的。(我怀疑这可能是我下面描述的问题的原因)

我想在piechart中为每个份额提供标签,而不是总数

在这个答案中,我找到了语法,它应该为图表添加标签

以下是我的脚本中的语法。如果未注释,带有geom_文本(用哈希注释)的行将导致我的绘图消失,并为所有小绘图创建一个长长的列表(16个条目),其中包含警告:

1: Removed 1 rows containing missing values (geom_col).
我认为原因可能是在准备数据的最后一行,将数据分组以进行绘图

我用散列标记的行是个问题。如果我把散列图放在正确的位置,如果我把它包括在内,试图在切片上获得所需的标签,那么图就会消失,或者是非常窄的垂直切片

       df.grobs <- df %>% 
      do(subplots = ggplot(., aes(1, sales, fill = component)) + 
           geom_bar(position = "fill", alpha = 0.5, colour = "white", stat="identity") + 
#          geom_text( aes(label = round(sales), y=sales), position = position_stack(vjust = 0.5), size = 2.5)  +
           coord_polar(theta = "y") + 
           scale_fill_manual(values = c("green", "red"))+
           theme_void()+ guides(fill = F)) %>% 
      mutate(subgrobs = list(annotation_custom(ggplotGrob(subplots), 
                                               x = lon-Potential_Sum/300, y = lat-Potential_Sum/300, 
                                               xmax = lon+Potential_Sum/300, ymax = lat+Potential_Sum/300))) 

    df.grobs


    df.grobs %>%
    {p + 
        .$subgrobs + 
                geom_col(data = df,
                 aes(0,0, fill = component), 
                 colour = "white")+ geom_text(data=df, aes(label = Miasto),nudge_y = -0.15, size=2.5)}
df.grobs%
do(子批次=ggplot(,aes(1,销售,填充=组件))+
几何图形条(position=“fill”,alpha=0.5,color=“white”,stat=“identity”)+
#几何图形文本(aes(标签=圆形(销售),y=销售),位置=位置堆栈(vjust=0.5),大小=2.5)+
极坐标(theta=“y”)+
刻度填充手动(数值=c(“绿色”、“红色”))+
theme_void()+辅助线(fill=F))%>%
mutate(subgrobs=list(注释)_custom(ggplotGrob(子地块)),
x=lon电位和/300,y=lat电位和/300,
xmax=lon+Potential_Sum/300,ymax=lat+Potential_Sum/300)
格罗布酒店
df.grobs%>%
{p+
.$subgrobs+
几何坐标(数据=df,
aes(0,0,填充=成分),
color=“white”)+geom_text(数据=df,aes(标签=Miasto),微移_y=-0.15,大小=2.5)}
为什么用散列标记的行(如果未注释)会破坏绘图而不是添加标签?它似乎完全重新定义了美学


编辑:我修改了标记行,现在label=salesy=sales。现在,如果我对行进行注释,将生成绘图,如果我取消注释,将在正确位置生成标签,但不会生成绘图。为什么我不能两者兼得?

简短回答:

我认为问题其实出在你之前的那句话中:

几何图形条(position=“fill”,alpha=0.5,color=“white”,stat=“identity”)+

如果您将位置从
fill
更改为
stack
(即默认值),它应该可以正常工作(至少在我的上是这样)

冗长的解释:

让我们使用mtcars数据集的摘要版本再现问题:

dfm <- mtcars %>% group_by(cyl) %>% summarise(disp = mean(disp)) %>% ungroup()

# correct pie chart
ggplot(dfm, aes(x = 1, y = disp, label = factor(cyl), fill = factor(cyl))) + 
  geom_bar(stat = "identity", position = "stack") + 
  geom_text(position = position_stack(vjust = 0.5)) + 
  coord_polar(theta = "y") + theme_void()

# "empty" pie chart
ggplot(dfm, aes(x = 1, y = disp, label = factor(cyl), fill = factor(cyl))) + 
  geom_bar(stat = "identity", position = "fill") + 
  geom_text(position = position_stack(vjust = 0.5)) + 
  coord_polar(theta = "y") + theme_void()

检查条形图的y轴。条和标签的位置正确

现在,带有
position=“fill”
的版本:


条形图现在占据y轴上的0-1范围,而标签继续占据原始的完整范围,该范围要大得多。因此,当您将图表转换为极坐标时,条形图会被压缩成一小块,几乎看不见。

谢谢。我承认我不太确定我在做什么,你的长篇大论解释了很多。现在我在我的国家地图上有了漂亮的小图表。令人惊叹的。你帮助了我,我相信其他人也会使用它!
       df.grobs <- df %>% 
      do(subplots = ggplot(., aes(1, sales, fill = component)) + 
           geom_bar(position = "fill", alpha = 0.5, colour = "white", stat="identity") + 
#          geom_text( aes(label = round(sales), y=sales), position = position_stack(vjust = 0.5), size = 2.5)  +
           coord_polar(theta = "y") + 
           scale_fill_manual(values = c("green", "red"))+
           theme_void()+ guides(fill = F)) %>% 
      mutate(subgrobs = list(annotation_custom(ggplotGrob(subplots), 
                                               x = lon-Potential_Sum/300, y = lat-Potential_Sum/300, 
                                               xmax = lon+Potential_Sum/300, ymax = lat+Potential_Sum/300))) 

    df.grobs


    df.grobs %>%
    {p + 
        .$subgrobs + 
                geom_col(data = df,
                 aes(0,0, fill = component), 
                 colour = "white")+ geom_text(data=df, aes(label = Miasto),nudge_y = -0.15, size=2.5)}
dfm <- mtcars %>% group_by(cyl) %>% summarise(disp = mean(disp)) %>% ungroup()

# correct pie chart
ggplot(dfm, aes(x = 1, y = disp, label = factor(cyl), fill = factor(cyl))) + 
  geom_bar(stat = "identity", position = "stack") + 
  geom_text(position = position_stack(vjust = 0.5)) + 
  coord_polar(theta = "y") + theme_void()

# "empty" pie chart
ggplot(dfm, aes(x = 1, y = disp, label = factor(cyl), fill = factor(cyl))) + 
  geom_bar(stat = "identity", position = "fill") + 
  geom_text(position = position_stack(vjust = 0.5)) + 
  coord_polar(theta = "y") + theme_void()
ggplot(dfm, aes(x = 1, y = disp, label = factor(cyl), fill = factor(cyl))) + 
  geom_bar(stat = "identity", position = "stack") + 
  geom_text(position = position_stack(vjust = 0.5))
ggplot(dfm, aes(x = 1, y = disp, label = factor(cyl), fill = factor(cyl))) + 
  geom_bar(stat = "identity", position = "fill") + 
  geom_text(position = position_stack(vjust = 0.5))