围绕轴标签添加圆圈
Adding circles around axis labels
我想在 y 轴上的某些标签周围画一个红色圆圈。我有以下数据和代码来绘制:
data <- structure(list(group = c("1", "1", "1", "2", "2", "2",
"3", "3", "3"), word = c("headache",
"computer", "window", "window", "plant", "coffee", "headache", "sky",
"computer"), n = c(115L, 125L, 130L, 132L, 134L, 157L,
195L, 209L, 215L), row = 1:9), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -9L))
#Define some colors to use throughout
my_colors <- c("#FFDBCE", "#8CAEAE", "#beb6d7")
wordfrequencyplot <- data %>%
ggplot(aes(row, n, fill =as.factor(group))) +#as factor primetype to make it discrete
geom_col(show.legend = F) +
labs(x = NULL, y = "Word Count") +
facet_wrap(~group, scales = "free_y","fixed_x") +
scale_x_continuous( # This handles replacement of row
breaks = data$row, # notice need to reuse data frame
labels = data$word) +
scale_fill_manual(values = my_colors)+
coord_flip()+
theme_bw()
我想在 y 轴上围绕第 2 组中的“咖啡”和“植物”以及第 3 组中的“天空”这两个词画圆圈。
可能有更好的方法来执行此操作,但您可以将 ggplot
对象转换为 grob
,然后使用 grid
中的 grid.circle
来放置圆圈.
library(grid)
library(ggplotify)
library(ggplot2)
wordfrequencyplot_grob <- as.grob(wordfrequencyplot)
plot(wordfrequencyplot_grob)
grid::grid.circle(x=0.365, y=0.784, r=0.039, default.units="npc", name=NULL,
gp=gpar(fill = NA, col = "red", lwd = 2), draw=TRUE, vp=NULL)
grid::grid.circle(x=0.367, y=0.511, r=0.034, default.units="npc", name=NULL,
gp=gpar(fill = NA, col = "red", lwd = 2), draw=TRUE, vp=NULL)
grid::grid.circle(x=0.706, y=0.511, r=0.026, default.units="npc", name=NULL,
gp=gpar(fill = NA, col = "red", lwd = 2), draw=TRUE, vp=NULL)
输出
与@Andrew Gillreath-Brown 发布的解决方案非常相似的解决方案是使用 cowplot
包在原始图上绘制圆圈。但是,您需要调整 circleGrob
的 x 和 y 值以获得所需的输出。
library(cowplot)
library(grid)
# Draw circles as Grob
g <- circleGrob(x = c(0.38, 0.38, 0.73),
y = c(0.51, 0.77, 0.51),
r = 0.05,
gp = gpar(fill = "transparent",
col = "red",
lwd = 3))
# Use ggdraw to add the circles over the original plot
ggdraw(wordfrequencyplot) +
# Draw circles Grob
draw_grob(g)
我想在 y 轴上的某些标签周围画一个红色圆圈。我有以下数据和代码来绘制:
data <- structure(list(group = c("1", "1", "1", "2", "2", "2",
"3", "3", "3"), word = c("headache",
"computer", "window", "window", "plant", "coffee", "headache", "sky",
"computer"), n = c(115L, 125L, 130L, 132L, 134L, 157L,
195L, 209L, 215L), row = 1:9), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -9L))
#Define some colors to use throughout
my_colors <- c("#FFDBCE", "#8CAEAE", "#beb6d7")
wordfrequencyplot <- data %>%
ggplot(aes(row, n, fill =as.factor(group))) +#as factor primetype to make it discrete
geom_col(show.legend = F) +
labs(x = NULL, y = "Word Count") +
facet_wrap(~group, scales = "free_y","fixed_x") +
scale_x_continuous( # This handles replacement of row
breaks = data$row, # notice need to reuse data frame
labels = data$word) +
scale_fill_manual(values = my_colors)+
coord_flip()+
theme_bw()
我想在 y 轴上围绕第 2 组中的“咖啡”和“植物”以及第 3 组中的“天空”这两个词画圆圈。
可能有更好的方法来执行此操作,但您可以将 ggplot
对象转换为 grob
,然后使用 grid
中的 grid.circle
来放置圆圈.
library(grid)
library(ggplotify)
library(ggplot2)
wordfrequencyplot_grob <- as.grob(wordfrequencyplot)
plot(wordfrequencyplot_grob)
grid::grid.circle(x=0.365, y=0.784, r=0.039, default.units="npc", name=NULL,
gp=gpar(fill = NA, col = "red", lwd = 2), draw=TRUE, vp=NULL)
grid::grid.circle(x=0.367, y=0.511, r=0.034, default.units="npc", name=NULL,
gp=gpar(fill = NA, col = "red", lwd = 2), draw=TRUE, vp=NULL)
grid::grid.circle(x=0.706, y=0.511, r=0.026, default.units="npc", name=NULL,
gp=gpar(fill = NA, col = "red", lwd = 2), draw=TRUE, vp=NULL)
输出
与@Andrew Gillreath-Brown 发布的解决方案非常相似的解决方案是使用 cowplot
包在原始图上绘制圆圈。但是,您需要调整 circleGrob
的 x 和 y 值以获得所需的输出。
library(cowplot)
library(grid)
# Draw circles as Grob
g <- circleGrob(x = c(0.38, 0.38, 0.73),
y = c(0.51, 0.77, 0.51),
r = 0.05,
gp = gpar(fill = "transparent",
col = "red",
lwd = 3))
# Use ggdraw to add the circles over the original plot
ggdraw(wordfrequencyplot) +
# Draw circles Grob
draw_grob(g)