看起来您正在尝试拉下您的 goodreads 数据,并根据开始数据、结束数据和书籍大小绘制出您在一年中阅读的书籍数量。
要执行您的建议,您可以使用parse
on 选项geom_text*(
,为此您必须创建一个解析字符串并将其作为输入sprintf()
传递给where 。geom_text*(
label
parse = TRUE
要添加换行符,您可以考虑使用plotmath::over()
parseLabel <- sprintf("over(%s,%s)",
gsub(" ", "~", books.2007$Title, fixed = TRUE),
gsub(" ", "~", books.2007$Author, fixed = TRUE))
parseLabel
或者,您可以使用下划线,但是添加换行符很棘手,因为plotmath()
不直接支持在解析公式中使用换行符。
parseLabel <- sprintf("underline(%s)~\n~%s",
gsub(" ", "~", books.2007$Title, fixed = TRUE),
gsub(" ", "~", books.2007$Author, fixed = TRUE))
parseLabel
注意:Baptiste在他的回答中正确地强调了这一点,我只是在使用我创建的示例数据集扩展他的工作。
好的,这是一个基于上述假设的快速示例。我希望这能为您指明正确的方向。
注意:我附加了一个示例数据集供人们使用。
添加下划线
为了给文本添加下划线,您可以通过在调用中设置来利用plotmath。parse=true
geom_label*()
使用带有 geom_label 的 plotmath 的简单示例
library(tidyverse) # Loads ggplot2
library(graphics)
library(ggrepel)
library(gtable)
library(ggalt)
# load test dataset
# ... See example data set
# books.2007 <- structure...
gp <- ggplot(books.2007)
gp <- gp + geom_dumbbell( aes(x = `Date Started`,
xend = `Date Finished`,
y = ISBN,
size = as.numeric(Pages)),
size_x = 0, size_xend = 0)
# Construct parseLabel using sprintf
parseLabel <- sprintf("underline(%s)~\n~%s",
gsub(" ", "~", books.2007$Title, fixed = TRUE),
gsub(" ", "~", books.2007$Author, fixed = TRUE))
gp <- gp + geom_label(aes(x = `Date Started`,
y = ISBN),
label = parseLabel,
vjust = 1.5, hjust = "inward", parse = TRUE)
gp <- gp + labs(size = "Book Size")
gp
示例绘图输出
使用带有 geom_label_repel 的 plotmath 的简单示例
注意。我个人的感觉是 geom_text 更容易使用,因为 geom_label_repel 需要计算开销来计算标签的位置。
## Construct parse string
##
##
parseLabel <- sprintf("underline(%s)~\n~%s",
gsub(" ", "~", books.2007$Title, fixed = TRUE),
gsub(" ", "~", books.2007$Author, fixed = TRUE))
parseLabel
rm(gp)
gp <- ggplot(books.2007)
gp <- gp + geom_dumbbell( aes(x = `Date Started`,
xend = `Date Finished`,
y = ISBN,
size = as.numeric(Pages)),
size_x = 0, size_xend = 0)
gp <- gp + geom_label_repel(aes(x = `Date Started`,
y = ISBN),
label = parseLabel,
# max.iter = 100,
parse = TRUE)
gp <- gp + labs(size = "Book Size")
gp
使用 geom_text_repel 的示例绘图输出
示例数据集:
books.2007 <- structure(list(Title = c("memoirs of a geisha", "Blink: The Power of Thinking Without Thinking",
"Power of One", "Harry Potter and the Half-Blood Prince (Book 6)",
"Dune (Dune Chronicles Book 1)"), Author = c("arthur golden",
"Malcolm Gladwell", "Bryce Courtenay", "J.K. Rowling", "Frank Herbert"
), ISBN = c("0099498189", "0316172324", "034541005X", "0439785960",
"0441172717"), `My Rating` = c(4L, 3L, 5L, 4L, 5L), `Average Rating` = c(4,
4.17, 5, 4.38, 4.55), Publisher = c("vintage", "Little Brown and Company",
"Ballantine Books", "Scholastic Paperbacks", "Ace"), Binding = c("paperback",
"Hardcover", "Paperback", "Paperback", "Paperback"), `Year Published` = c(2005L,
2005L, 1996L, 2006L, 1990L), `Original Publication Year` = c(2005L,
2005L, 1996L, 2006L, 1977L), `Date Read` = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_), `Date Added` = structure(c(13558,
13558, 13558, 13558, 13558), class = "Date"), Bookshelves = c("fiction",
"nonfiction marketing", "fiction", "fiction fantasy", "fiction scifi"
), `My Review` = c(NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_), `Date Started` = structure(c(13577,
13610, 13634, 13684, 13722), class = "Date"), `Date Finished` = structure(c(13623,
13647, 13660, 13689, 13784), class = "Date"), Pages = c("522",
"700", "300", "145", "700")), .Names = c("Title", "Author", "ISBN",
"My Rating", "Average Rating", "Publisher", "Binding", "Year Published",
"Original Publication Year", "Date Read", "Date Added", "Bookshelves",
"My Review", "Date Started", "Date Finished", "Pages"), row.names = c(NA,
-5L), spec = structure(list(cols = structure(list(Title = structure(list(), class = c("collector_character",
"collector")), Author = structure(list(), class = c("collector_character",
"collector")), ISBN = structure(list(), class = c("collector_character",
"collector")), `My Rating` = structure(list(), class = c("collector_integer",
"collector")), `Average Rating` = structure(list(), class = c("collector_double",
"collector")), Publisher = structure(list(), class = c("collector_character",
"collector")), Binding = structure(list(), class = c("collector_character",
"collector")), `Year Published` = structure(list(), class = c("collector_integer",
"collector")), `Original Publication Year` = structure(list(), class = c("collector_integer",
"collector")), `Date Read` = structure(list(), class = c("collector_character",
"collector")), `Date Added` = structure(list(), class = c("collector_character",
"collector")), Bookshelves = structure(list(), class = c("collector_character",
"collector")), `My Review` = structure(list(), class = c("collector_character",
"collector"))), .Names = c("Title", "Author", "ISBN", "My Rating",
"Average Rating", "Publisher", "Binding", "Year Published", "Original Publication Year",
"Date Read", "Date Added", "Bookshelves", "My Review")), default = structure(list(), class = c("collector_guess",
"collector"))), .Names = c("cols", "default"), class = "col_spec"), class = c("tbl_df",
"tbl", "data.frame"))
简单示例 - 无格式
为了完整起见,我将如何解决避免公式构造问题的问题。
gp <- ggplot(books.2007)
gp <- gp + geom_dumbbell( aes(x = `Date Started`,
xend = `Date Finished`,
y = ISBN,
size = as.numeric(Pages)),
size_x = 0, size_xend = 0)
t <- paste(books.2007$Title, "\n", books.2007$Author)
gp <- gp + geom_label(aes(x = `Date Started`,
y = ISBN),
label = t,
vjust = 1.5, hjust = "inward", parse = FALSE)
gp <- gp + labs(size = "Book Size")
gp
绘图输出