标签: geom-text

在R/ggplot中没有得到geom_text

我有一个ggplot,它自己工作得很好.但是当我尝试将它导入到情节api系统时,geom_text似乎不起作用 - 其他一切都有效.谁能帮我?

这是我的R版本 - R版本3.1.2(2014-10-31)和情节版本 - 0.5.23

我使用的数据是在file.csv中,看起来像:

Province,Community,General Shelters,General Beds,Mens Shelters,Mens Beds,Womens Shelters,Womens Beds,Youth Shelters,Youth Beds,Family Shelters,Family Beds,Total Shelters,Total Beds
New Brunswick,Saint John,0,0,1,35,1,10,0,0,0,0,2,45
Quebec,Montréal,7,114,9,916,12,259,17,197,1,7,45,"1,493"
Quebec,Québec City,3,49,2,102,1,12,2,15,0,0,8,178
Ontario,Toronto,4,250,13,"1,483",10,572,10,416,4,496,41,"3,217"
British Columbia,Vancouver,13,545,7,291,9,238,7,90,2,30,38,"1,194"
British Columbia,Victoria,1,84,1,21,1,25,1,10,1,5,5,145
Run Code Online (Sandbox Code Playgroud)

这是我的完整代码:

library(ggplot2)
library(zoo)
library(DAAG)
library(mapdata) #for canada map from worldhires database
library(ggmap)
library("plotly") # for plotly

homeless <- function()
{
    allcit <- NULL

    #read csv
    allcittmp <- read.csv("file.csv", sep=",", header=TRUE, colClasses="character")

    #cast data to proper format from character for both data frames
    allcittmp[,1] <- as.character(allcittmp[,1]) …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 plotly geom-text

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

如何更改绘图区域外 geom_text() 中的字体大小?

我想对绘图进行注释,并且希望注释位于绘图区域之外。我找到了这个解决方案,它适用于在绘图区域之外添加注释,但我无法弄清楚如何更改标签的外观(最重要的是,就我的目的而言,字体大小)。

这是上述解决方案的一个最小示例:

library (ggplot2)
library(grid)

df=data.frame(y=c("dog1","dog2","dog3"),x=c(12,10,14),n=c(5,15,20))
p <- ggplot(df, aes(x,y)) + geom_point()

# Add the annotation
p <- p + geom_text(aes(label = "Hello World!", x = 0, y = 0), vjust = 2, hjust = 1)

# Code to override clipping
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)
Run Code Online (Sandbox Code Playgroud)

理想情况下,注释应位于左下角。

plot r ggplot2 geom-text

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

如何在ggplot中添加带斜体和变量的复杂标签?

我一直在使用阅读关于这一主题的贴子很多expression(),paste()bquote(),或某种组合.我想我已接近解决我的问题,但我无法到达那里.以下脚本生成标有"y = 1 + 2(x); r ^ 2 = 0.9"的图.我怎样才能将"y"和"x"斜体化,并将"r"和上标"r ^ 2"的斜体标记为斜体?如果我忽略了相关的早期帖子,抱歉,请指导我.

df <- data.frame(x=c(1:5), y=c(1:5))
a <- 1    
b <- 2
r2 <- 0.9
eq <- paste("y = ", a, " + ", b, "(x); r^2=", r2)
ggplot(data=df, aes(x=x, y=y))+
  geom_point(color="black")+
  geom_text(x=2, y=4,label=eq, parse=FALSE)
Run Code Online (Sandbox Code Playgroud)

expression r ggplot2 geom-text

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

使用 geom_text 在 ggplot2 中的各个方面注释文本

使用以下代码,我获得每个面上的文本,但文本是叠加的:“X1”和“X2”叠加在每个面上。我的代码问题出在哪里?

R代码:

new_df <- data.frame(f = as.factor(rep(1:2, 15)), x = rnorm(30), y = runif(30))
g <- ggplot(data = new_df, aes(x = x, y = y)) + geom_point() 
g <- g + facet_grid(. ~ f)
g
label_graph <- data.frame(label = c("X1", "X2"))

g <- g + geom_text(data = label_graph,
                    mapping = aes(x = Inf, y = -Inf, label = label),
                    hjust = 1.1, vjust = -1.1)
g
Run Code Online (Sandbox Code Playgroud)

label r ggplot2 geom-text

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

如何阻止 R ggplot 将“a”显示为图例中的符号?

最近,我在 R 中使用 ggplot 绘制了一个图。当我添加 ggplot_label_repel 的代码时,图例中的符号突然从圆圈变为字母“a”。

这和wingdings字体有关系吗?如何修复我的图例?

我在这里制作了一些带有可重现错误的示例代码:

#packages
library(dplyr)
library(ggplot2)
library(ggrepel)
#Creating an example data frame
years <- c(1990:1999)
population <- c(1:10)
df <- tibble(years,population)

#Adding random information to the df so that I can do coloring and labeling on my ggplot
df$group <- "low population"
df[8:10, 3] <- "high population"
df$status <- "highly endangered"
df$status[3:5] <- "endangered"
df$status[5:7] <- "threatened"
df$status[8:10] <- "recovered"

#The ggplot with the legend I want
ggplot(df, aes(years,population, color= group))+
  geom_point()

#The ggplot …
Run Code Online (Sandbox Code Playgroud)

r legend ggplot2 geom-text

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

删除ggplot2中标签/文本周围的段

考虑一个带有线段/线和文本/标签的图。我希望文本覆盖该段,以便文本不与该段重叠。

我尝试使用,geom_label但我仍然希望背景相同,只是删除文本周围的其他对象。我也尝试过,geomtextpath但无法使文本水平。有任何想法吗?

seg <- data.frame(x = 1, xend = 1, y = 2, yend = 3)
plot1 <- ggplot(seg) +
  aes(x = x, xend = xend, y = y, yend = yend) +
  geom_segment() +
  geom_text(aes(y = (y + yend) / 2), label = "Hello", size = 10) +
  labs(title = "geom_text", 
       subtitle = "The text is horizontal but the segment should\nbe cut around the text.")

library(geomtextpath)
plot2 <- ggplot(seg) +
  aes(x = x, xend = …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 geom-text

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

使用 geom_dl() 重叠标签

我的线图很合适,只是我的标签重叠太多。我意识到它们各自的数据点很接近,但是有没有办法美化标签间距?

我的数据和代码:

library(ggplot2)
library(directlabels) #geom_dl

#Original df
df <- data.frame(names=c('AAAA', 'BBBB', 'CCCC', 'DDDD', 'EEEE'),SP22=c(57, 30, 27, 35, 34),FA22=c(52, 38, 31, 34, 31),SP23=c(49, 32, 30, 29, 31))

#df to long format
longdf <- melt(df, id='names')

last.bumpup <- list("last.points","bumpup")

#lineplot
longdf %>%
ggplot() + 
  geom_line(aes(x=variable, y=value, group=names, color=names)) +
  scale_y_continuous(n.breaks=6, limits=c(20, 60)) +
  scale_color_manual(values=c("darkred", "steelblue","black", "coral1", 
                              "darkorchid2")) +
  geom_dl(aes(x=variable, y=value,label=names, color= names),
          method="last.bumpup", cex=0.8) +
  labs(title = "Comparison of...") + 
  xlab(label = "Terms") +
  ylab(label = "Numbers") +
  #scale_colour_discrete(guide="none") +
  theme_minimal() + …
Run Code Online (Sandbox Code Playgroud)

label r ggplot2 geom-text

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

使用R在ggplot2中自定义geom文本颜色

我想用ggplot可视化网球数据.到目前为止,我能够根据赢家/输家数据可视化数据.

2015    Flavia Pennetta 81
2014    Serena Williams 65
2013    Serena Williams 109
2012    Serena Williams 94
2011    Samantha Stosur 61
2010    Kim Clijsters   58
2009    Kim Clijsters   82
2008    Serena Williams 89
2007    Justine Henin   70
2006    Maria Sharapova 66
2015    Roberta Vinci   47
2014    Caroline Wozniacki  49
2013    Victoria Azarenka   91
2012    Victoria Azarenka   87
2011    Serena Williams 41
2010    Vera Zvonareva  31
2009    Caroline Wozniacki  66
2008    Jelena Jankovic 79
2007    Svetlana Kuznetsova 54
2006    Justine Henin …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 geom-text

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

如何将 adjustment_text 与plotnine 一起使用?

我正在尝试调整 geom_text 标签,使它们不重叠。使用 nudge_x、nudge_y 和 size 调整标签并没有给我一个非常满意的结果。我遇到了 adjustment_text 包,但我无法将其实现到我的代码中。

现在我的图表看起来像这样:

图形

我尝试添加 adjustment_text =True (见下文),这给了我以下错误。“几何、统计或图层都无法理解参数{'调整文本'}。”

(ggplot(nutrient_rel_item, aes(y='Share_calories', x='Share_biomass', color='Type',show_legend=False))
+stat_smooth( aes(y='Share_calories', x='Share_biomass'),method='lm',inherit_aes=False) 
+geom_text(aes(label='Abb'),data=nutrient_rel_item, nudge_x=0.1, nudge_y=0.1, size=4, adjust_text =True)
+facet_wrap('~CCC1',nrow=2)
+ scale_x_log10(labels=lambda l: ["%d%%" % (v * 100) for v in l])
+ scale_y_log10(labels=lambda l: ["%d%%" % (v * 100) for v in l])
+ geom_point(size=0.1) 
+ geom_path(aes(group='Item'), arrow= arrow(angle = 15, length= 0.1, type = "closed"))
+ labs(x='x', y='y')
+ theme(legend_position='none'))
Run Code Online (Sandbox Code Playgroud)

pandas geom-text plotnine

4
推荐指数
1
解决办法
2578
查看次数

geom_text() 的边界位置

我正在制作倾斜条形图的几个实例。由于计数大小和百分比差异不同,在某些情况下,其中一个标签(计数)的一部分会被推到栏外。在所有情况下,我需要标签完全位于栏内。如果没有重新定位以适合条形图,我需要将标签按原样居中。

在此输入图像描述

代码是:

library(tidyverse)
library(ggplot2)

data <- tibble(type = c('Cat', 'Dog'),
               group = c('Pets', 'Pets'),
               count = c(10000, 990000),
               percent = c(1, 99))

ggplot(data, aes(x = group, y = percent, fill = type)) +
  geom_bar(stat = 'identity', 
           position = position_stack(reverse = TRUE)) +
  coord_flip() +
  geom_text(aes(label = count),
            position = position_stack(vjust = 0.5, 
                                      reverse = TRUE))
Run Code Online (Sandbox Code Playgroud)

r ggplot2 geom-text

4
推荐指数
1
解决办法
1060
查看次数

标签 统计

geom-text ×10

ggplot2 ×9

r ×9

label ×2

expression ×1

legend ×1

pandas ×1

plot ×1

plotly ×1

plotnine ×1