相关疑难解决方法(0)

将geom_text的默认"a"图例更改为标签字符串本身

此问题类似,我想更改图例中的默认"a",而不是完全删除它,我想用标签本身替换它.也就是说,图例的第一行应该有一个标有"se"的彩色图标,右边是全名"setosa".

iris$abbrev = substr( iris$Species, 1, 2 )

ggplot(data = iris, aes(x = Sepal.Length, y=Sepal.Width, shape =
Species, colour = Species)) +  geom_text(aes(label = abbrev))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

r legend ggplot2 geom-text

16
推荐指数
2
解决办法
384
查看次数

如何在 ggplot2 图例中显示向后和向前的箭头?

我有一个dataframe带有箭头坐标的:

arrows = data.frame(direction = factor(c("forward", "backward", "backward")),
                x = c(0, 0, 0),
                xend = c(1, 1, 1),
                y = c(1, 1.2, 1.1),
                yend = c(1, 1.2, 1.1))
Run Code Online (Sandbox Code Playgroud)

我想使用这些数据来绘制箭头 - 有些是向前的,反之亦然。

到目前为止我尝试过:

library(ggplot2)

ggplot() +
        geom_segment(data = arrows, 
                     aes(x, y, xend = xend, yend = yend, col = direction),
                     arrow = arrow(length = unit(0.3, "cm"), type = "closed", ends = c("last", "first")))
Run Code Online (Sandbox Code Playgroud)

其产生:

在此输入图像描述

我想在这个图中解决两个问题:

  1. 我如何确保ggplot“理解”哪个情节是“向前”和“向后”,以便为他们提供正确的图例?

  2. 如何更改图例,使其显示前进方向,并带有向前的箭头和向后的向后箭头?

r ggplot2

7
推荐指数
1
解决办法
725
查看次数

旋转geom_abline的图例键

我想将图例键旋转为geom_abline水平图例键,或者如果可能的话根据其斜率旋转。这是一些可重现的代码(dput下面的 df ):

library(ggplot2)
p = ggplot(df, mapping = aes(x = x, y = y)) +
  geom_point() +
  geom_abline(df, mapping = aes(slope = slope, intercept = 0, color = factor(group))) +
  coord_cartesian(xlim = c(0, 3), ylim = c(0, 3)) 
p
Run Code Online (Sandbox Code Playgroud)

创建于 2023-04-19,使用reprex v2.0.2

我尝试使用 @Sandy Muspratt 他的答案在这里grid功能,但这也旋转了 ablines 和图例键。所以我想知道是否有人知道如何旋转 的图例键geom_abline


dputdf:

df<-structure(list(x = c(0, 0, 0), y = c(0, 0, 0), slope = c(0.5, 
1, 0.75), group = …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

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

使用表情符号的 ggplot 图例

我有下面的代码,可以生成饼图和图例。有没有办法让表情符号和饼图具有相同的对应颜色,因为目前表情符号是黑色的。另外,我希望删除图例中的方形色块,并且术语“男性”和“女性”出现在正确的表情符号旁边

library(tidyverse)
library(emojifont)
load.emojifont("OpenSansEmoji.ttf")

gender<-data.frame(`Gender`=c("Male","Female"),
                   `Proportion`=c(45,55), symbol=c("\UF182","\UF183"))

ggplot(gender, aes(x="", Proportion, fill=symbol, label=symbol)) +
  geom_bar(width = 1, stat = "identity")+
  coord_polar("y", start=0)+
  xlab(NULL)+ylab(NULL) +
  theme(legend.text=element_text(family='fontawesome-webfont'))
Run Code Online (Sandbox Code Playgroud)

r ggplot2 emoji

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

标签 统计

ggplot2 ×4

r ×4

emoji ×1

geom-text ×1

legend ×1