甜甜圈图的ggplot标签放置
ggplot label placement for doughnut plot
我决定违背一般规则,制作一个甜甜圈图。情节本身很好(相当均匀),但我似乎无法将标签放在正确的位置。我敢打赌这是微不足道的,但我就是看不到它。
代码如下:
mdat <- data.frame(
category = c("C", "E", "I", "L", "Mi", "Mo",
"O", "Q", "S", "V"),
ct = c(147, 275, 431, 967, 121, 105, 17, 186, 620, 42))
mdat$category <- factor(mdat$category, levels = mdat$category)
# Fractions
mdat$fraction <- mdat$ct / sum(mdat$ct)
# Cumulative fractions; this forms the top of each rectangle
mdat$ymax <- cumsum(mdat$fraction)
# This will the the bottom of the rectangle
mdat$ymin <- c(0, head(mdat$ymax, n = -1))
# Label position - this isn't right
mdat$labelPosition <- ((mdat$ymax + mdat$ymin) / 2)
# Labels
mdat$label <- paste0(mdat$category, " Fraction: \n",
round(mdat$ct/sum(mdat$ct), 4) * 100, "%")
# Plot
g <- ggplot(mdat, aes(ymax = ymax, ymin = ymin, xmax = 11, xmin = 10,
fill = category))
g <- g + geom_rect()
g <- g + geom_label(x = 2, aes(y = labelPosition, label = label), size = 3)
g <- g + scale_fill_brewer(palette = "Set3")
g <- g + scale_color_brewer(palette = "Set3")
g <- g + coord_polar(theta = "y")
g <- g + xlim(c(7, 12))
g <- g + theme_void()
g <- g + theme(legend.position = "none")
g
这是代码生成的图:
它们可能会因部分轮换而关闭?我就是没看到。
谢谢。
圆环图只是极坐标中的堆积条形图。因此,如果您在 xlim() 的下边界下方的 geom_label() 内选择 x,则标签会漂移到绘图的另一侧。
g <- ggplot(mdat, aes(ymax = ymax, ymin = ymin, xmax = 11, xmin = 10,
fill = category))
g <- g + geom_rect()
g <- g + geom_label(x = 12.1, aes(y = labelPosition, label = label), size = 3)
g <- g + scale_fill_brewer(palette = "Set3")
g <- g + scale_color_brewer(palette = "Set3")
g <- g + coord_polar(theta = "y")
g <- g + xlim(c(7, 12))
g <- g + theme_void()
g <- g + theme(legend.position = "none")
g
我决定违背一般规则,制作一个甜甜圈图。情节本身很好(相当均匀),但我似乎无法将标签放在正确的位置。我敢打赌这是微不足道的,但我就是看不到它。
代码如下:
mdat <- data.frame(
category = c("C", "E", "I", "L", "Mi", "Mo",
"O", "Q", "S", "V"),
ct = c(147, 275, 431, 967, 121, 105, 17, 186, 620, 42))
mdat$category <- factor(mdat$category, levels = mdat$category)
# Fractions
mdat$fraction <- mdat$ct / sum(mdat$ct)
# Cumulative fractions; this forms the top of each rectangle
mdat$ymax <- cumsum(mdat$fraction)
# This will the the bottom of the rectangle
mdat$ymin <- c(0, head(mdat$ymax, n = -1))
# Label position - this isn't right
mdat$labelPosition <- ((mdat$ymax + mdat$ymin) / 2)
# Labels
mdat$label <- paste0(mdat$category, " Fraction: \n",
round(mdat$ct/sum(mdat$ct), 4) * 100, "%")
# Plot
g <- ggplot(mdat, aes(ymax = ymax, ymin = ymin, xmax = 11, xmin = 10,
fill = category))
g <- g + geom_rect()
g <- g + geom_label(x = 2, aes(y = labelPosition, label = label), size = 3)
g <- g + scale_fill_brewer(palette = "Set3")
g <- g + scale_color_brewer(palette = "Set3")
g <- g + coord_polar(theta = "y")
g <- g + xlim(c(7, 12))
g <- g + theme_void()
g <- g + theme(legend.position = "none")
g
这是代码生成的图:
它们可能会因部分轮换而关闭?我就是没看到。
谢谢。
圆环图只是极坐标中的堆积条形图。因此,如果您在 xlim() 的下边界下方的 geom_label() 内选择 x,则标签会漂移到绘图的另一侧。
g <- ggplot(mdat, aes(ymax = ymax, ymin = ymin, xmax = 11, xmin = 10,
fill = category))
g <- g + geom_rect()
g <- g + geom_label(x = 12.1, aes(y = labelPosition, label = label), size = 3)
g <- g + scale_fill_brewer(palette = "Set3")
g <- g + scale_color_brewer(palette = "Set3")
g <- g + coord_polar(theta = "y")
g <- g + xlim(c(7, 12))
g <- g + theme_void()
g <- g + theme(legend.position = "none")
g