I有以下图,其中辅助轴显示了行和列的总数。我想将它们标记为“总”。我知道我可以通过更改
name = NULL
获得一个轴标题,但这只会出现在数字右下方的下方 /下方。我希望它们与其他轴标签保持一致。我也想显示右下角的总数。
[![plot] [1]] https://i.sstatic.net/gpbmwoiz.jpg
## Code for the above plot
library(ggplot2)
# Create example data
DATA <- data.frame(
"Top" = factor(rep(c("A", "B", "C"), each = 3)),
"Side" = factor(rep(c("Blue", "Green", "Red"), times = 3)),
"Count" = c(2,13,0,4,1,10,15,0,6))
# Calculate row and column totals
COL.TOTALS <- aggregate(DATA$Count, by = list(DATA$Top), FUN = sum)
ROW.TOTALS <- aggregate(DATA$Count, by = list(DATA$Side), FUN = sum)
# Plot
ggplot(DATA, aes(as.numeric(Top), as.numeric(Side), Count)) +
geom_point(aes(size = Count)) +
scale_x_continuous(position = "top", breaks = 1:3,
labels = levels(DATA$Top), sec.axis = dup_axis(breaks =
as.numeric(COL.TOTALS$Group.1), labels = COL.TOTALS$x,
name = NULL)) +
scale_y_continuous(breaks = 1:3, labels = levels(DATA$Side),
sec.axis = dup_axis(breaks = as.numeric(ROW.TOTALS$Group.1),
labels = ROW.TOTALS$x, name = NULL))
r
Copy
Edit
library(ggplot2)
# Create example data
DATA <- data.frame(
Top = factor(rep(c("A", "B", "C"), each = 3)),
Side = factor(rep(c("Blue", "Green", "Red"), times = 3)),
Count = c(2, 13, 0, 4, 1, 10, 15, 0, 6)
)
# Calculate row and column totals
COL_TOTALS <- aggregate(Count ~ Top, data = DATA, FUN = sum)
ROW_TOTALS <- aggregate(Count ~ Side, data = DATA, FUN = sum)
overall_total <- sum(DATA$Count) # overall total
# Base plot: primary axes show factor labels; secondary axes show totals
p <- ggplot(DATA, aes(as.numeric(Top), as.numeric(Side))) +
geom_point(aes(size = Count)) +
scale_x_continuous(
position = "top",
breaks = 1:3,
labels = levels(DATA$Top),
sec.axis = dup_axis(
breaks = 1:3,
labels = COL_TOTALS$Count, # column totals
name = NULL # no built-in title
)
) +
scale_y_continuous(
breaks = 1:3,
labels = levels(DATA$Side),
sec.axis = dup_axis(
breaks = 1:3,
labels = ROW_TOTALS$Count, # row totals
name = NULL # no built-in title
)
) +
theme_minimal() +
# Remove any automatic axis titles so we can add our own annotations
theme(
axis.title.x.bottom = element_blank(),
axis.title.y.right = element_blank()
)
# Now add text annotations for the extra labels.
# (Positions here are in data coordinates and may need adjustment.)
p +
# For the column totals, add a "Total" label to the right of the ticks.
annotate("text", x = 3.5, y = 1, label = "Total", hjust = 0, size = 4) +
# For the row totals, add a "Total" label below the ticks.
annotate("text", x = 1, y = 0.5, label = "Total", hjust = 1, size = 4) +
# Finally, add the overall total in the lower right corner.
annotate("text", x = 3.5, y = 0.5, label = overall_total, hjust = 0, size = 4)
解释
没有标题的次要轴:
在每个dup_axis()中,我们设置名称= null,以便ggplot2不绘制默认标题。这意味着只出现tick标签(总数)。
注入“总”标签: 我们使用注释(“文本”,...)添加列总计的“总”标签(放置在次级X轴的右侧)和行总计(放置在次级Y轴下方)。根据需要调整X和Y坐标(以及使用Hjust/Vjust的理由),以使这些标签与您的其他轴标签对齐。
总计: 同样,我们在绘图区域的右下方添加了另一个文本注释(此处显示总数,这是所有计数的总和)。取决于您的实际情节维度和所需的美学,您可能需要细化坐标和文本大小。该解决方案“硬编码”数据坐标中的位置;另一种方法是使用网格注释(通过Annotation_custom())将文本相对于视口定位。但是,上述解决方案通常在许多情况下都很好。