- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在不明显延伸 x 轴的情况下垂直对齐折线图上的 geom_label。有没有一种方法可以在图表右侧留出空白,以便 ggrepel
函数(下方)有工作空间?
我正在尝试复制 this 上的最后一张图表Karam Belkar 的帖子(请参阅帖子底部的代码)除了没有 facet_wrap
并使用 ggplot
中的 economic
示例数据集。
当我使用 expand_limits
时得到错误信息:
Error: Invalid input: date_trans works with objects of class Date only
但是economics$date是Date格式的!
library("tidyverse")
library("ggthemes")
library("ggrepel")
df1 <- gather(economics, variable_name, observation, -date) %>%
rename(period = date) %>%
filter(variable_name %in% c("pce", "unemploy"))
p <- ggplot(df1, aes(x = period, y = observation, colour = variable_name)) +
geom_line() +
coord_cartesian(xlim = c(min(df1$period), max(df1$period))) +
#Alternative line to that above with undesirable result
#coord_cartesian(xlim = c(min(df1$period), max(df1$period) **+ 3000**)) +
geom_text_repel(
data = subset(df1, period == max(period)),
aes(label = variable_name),
size = 3,
nudge_x = 45,
segment.color = 'grey80'
) +
scale_y_continuous(labels = scales::comma) +
theme_minimal(base_size = 16) +
scale_color_tableau() +
scale_fill_tableau() +
theme(legend.position = 'none') +
labs(x="", y="", title = "Economic Data")
p + expand_limits(x = 700)
#the data set has 574 observations so I tried to
#add another 126 to give space to the labels
ggrepel
Usage Examples page在“线图”标题下有一些基于橙树生长数据的示例。这涉及在 coord_cartesian
函数中添加一个 x
值以增加 x 轴。这为我需要的标签行为提供了空间,但这意味着 x 轴延伸到 2020 年之后,图表的该部分上方没有数据,这是不可取的。
最佳答案
有必要以日期格式提供轴限制,使用 expand_limits(x=700)
将其关闭,下面的工作将其合并到 scale_x_date
中。我使用 1200 而不是 700 来创建附加图表。
p <- ggplot(df1, aes(x = period, y = observation, colour = variable_name)) +
geom_line() +
geom_text_repel(
data = subset(df1, period == max(as.Date(period, "%Y-%m-%d"))),
aes(label = variable_name),
size = 3,
nudge_x = 45,
segment.color = 'grey80'
) +
scale_y_continuous(labels = scales::comma) +
theme_minimal(base_size = 16) +
scale_color_tableau() +
scale_fill_tableau() +
theme(legend.position = 'none') +
labs(x="", y="", title = "Economic Data")
p + scale_x_date(limits = c(min(df1$period), max(df1$period) + 1200))
关于r - 垂直对齐 geom_label 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49607324/
我想在 geom_label 中绘制一个上标的星星来自数据框字符串,但找不到解决方案。 一个最小的工作示例是 library(ggplot2) df <- data.frame(x = 1, y =
问题 我试图在不明显延伸 x 轴的情况下垂直对齐折线图上的 geom_label。有没有一种方法可以在图表右侧留出空白,以便 ggrepel 函数(下方)有工作空间? 我正在尝试复制 this 上的最
有没有办法格式化 geom_label 打印的日期?在 ggplot2超出它们的原始值(value)?在下面的示例中,日期打印为“1910-01-01”。但是,假设我只想打印月份和日期,并将月份作为文
由于某些原因,当您使用 geom_label 时,ggplot 会向实际点添加边框,但不会在图例中包含边框。一个简单的例子: library(tidyverse) mtcars %>% rowna
在构建我的 Shiny 应用程序时,我遇到了 ggplot2 图形在不同窗口大小下看起来非常不同的情况。第一张图以完整的桌面大小显示了情节——一切都很好: 但是,当我更改输出窗口的大小时,每个元素似乎
由于某些原因,当您使用 geom_label 时,ggplot 会向实际点添加边框,但不会在图例中包含边框。一个简单的例子: library(tidyverse) mtcars %>% rowna
当尝试将 geom_label() 框的左上角与 ggplot2 的 (0,10) 坐标对齐时,文本不再居中(见图)。是否可以有一个左上对齐的框,里面的文本居中? library(ggplot2) d
我的数据: dat % as.numeric()))) 但是,当我将这个表达式与 ggplot 一起使用时: ggplot() + geom_point(data = dat, aes(x = x
我在关注 this ggplot2 docs尝试使用 geom_label 重现文本标签.但我收到一条错误消息, "could not find function geom_label" . ?geo
在 R 中为我的 (gg) 图使用 geom_label 时,我注意到文本上方的空间比下方的空间大。如何将文本对齐到标签框的中间? x <- data.frame(x = c("Being not",
我已经阅读了以下文章: https://trinkerrstuff.wordpress.com/2018/03/15/2246/ 现在我正在尝试使用建议的方法 bquote在我的情节中。但是我无法让它
我想做的是,向使用 geom_point 制作的点添加标签,这些点具有映射到大小的变量。该值也显示在标签中。文本或标签应位于 geom_point 的正下方,但使用position_nudge 似乎不
我想做的是,向使用 geom_point 制作的点添加标签,这些点具有映射到大小的变量。该值也显示在标签中。文本或标签应位于 geom_point 的正下方,但使用position_nudge 似乎不
我想创建一个直方图,其中有一条垂直线表示平均值,并在该线上附加一个标签,给出平均值的精确值。 我可以轻松创建带有垂直线的基本直方图。 # needed library library(ggplot2)
我想在 geom_label 中使用“尺寸”作为美学.我的问题是最小标签尺寸太小以至于相应的标签几乎无法阅读。 在使用尺寸美学时,有没有办法设置“最小尺寸”之类的东西?如果有办法,它也应该可以指定最大
我是一名优秀的程序员,十分优秀!