不同组中具有重复元素的棒棒糖图表
Lollipop chart with repeated elements in different groups
我正在尝试绘制一个包含 5 个组和这些组中重复元素的棒棒糖图表。如果所有元素都有不同的名称,它会按预期工作:
预期行为:
问题是我只想在不同的组中绘制 5 个算法,当我实际从算法 1-5 中命名它们时,绘图会发生这种情况:
意外行为:
这是我生成棒棒糖图表的正确行为的代码片段(除了错误的标签):
library(ggpubr)
# Create dataset
data <- data.frame(
algorithm=paste( "Algorithm ", seq(1,25), sep=""),
category=as.factor(c( rep('A', 5), rep('B', 5), rep('C', 5), rep('D', 5), rep('E', 5))),
metric=c(rep(rev(96:100), 5))
)
ggdotchart(data, x = "algorithm", y = "metric",
color = "category", # Color by groups
palette = c("#264653", "#2a9d8f", "#e9c46a", "#f4a261", "#e76f51"), # Custom color palette
sorting = "descending", # Sort value in descending order
add = "segments", # Add segments from y = 0 to dots
rotate = TRUE, # Rotate vertically
group = "category", # Order by groups
dot.size = 7, # Large dot size
label = round(data$metric), # Add mpg values as dot labels
font.label = list(color = "white", size = 8,
vjust = 0.5), # Adjust label parameters
ggtheme = theme_pubr() # ggplot2 theme
) +
labs(y = "Metric (%)", color="")
这是导致此行为的新数据片段:
# Create dataset
data <- data.frame(
algorithm=rep(paste( "Algorithm ", seq(1,5), sep=""), 5),
category=as.factor(c( rep('A', 5), rep('B', 5), rep('C', 5), rep('D', 5), rep('E', 5))),
metric=c(rep(rev(96:100), 5))
)
我怎样才能解决这个问题?
生成后,我们可以像编辑任何其他 ggplot
对象一样对其进行编辑。我们可以使用 scale_x_discrete()
来操纵轴标签,这避免了与 ggdotchart()
的原始绘图定义和构造的任何混淆。使用您的第一个情节 p
,我们可以:
alg_labels <- rep(paste( "Algorithm ", seq(1,5), sep=""), 5)
p +
scale_x_discrete(
labels = alg_labels
)
我正在尝试绘制一个包含 5 个组和这些组中重复元素的棒棒糖图表。如果所有元素都有不同的名称,它会按预期工作:
预期行为:
问题是我只想在不同的组中绘制 5 个算法,当我实际从算法 1-5 中命名它们时,绘图会发生这种情况:
意外行为:
这是我生成棒棒糖图表的正确行为的代码片段(除了错误的标签):
library(ggpubr)
# Create dataset
data <- data.frame(
algorithm=paste( "Algorithm ", seq(1,25), sep=""),
category=as.factor(c( rep('A', 5), rep('B', 5), rep('C', 5), rep('D', 5), rep('E', 5))),
metric=c(rep(rev(96:100), 5))
)
ggdotchart(data, x = "algorithm", y = "metric",
color = "category", # Color by groups
palette = c("#264653", "#2a9d8f", "#e9c46a", "#f4a261", "#e76f51"), # Custom color palette
sorting = "descending", # Sort value in descending order
add = "segments", # Add segments from y = 0 to dots
rotate = TRUE, # Rotate vertically
group = "category", # Order by groups
dot.size = 7, # Large dot size
label = round(data$metric), # Add mpg values as dot labels
font.label = list(color = "white", size = 8,
vjust = 0.5), # Adjust label parameters
ggtheme = theme_pubr() # ggplot2 theme
) +
labs(y = "Metric (%)", color="")
这是导致此行为的新数据片段:
# Create dataset
data <- data.frame(
algorithm=rep(paste( "Algorithm ", seq(1,5), sep=""), 5),
category=as.factor(c( rep('A', 5), rep('B', 5), rep('C', 5), rep('D', 5), rep('E', 5))),
metric=c(rep(rev(96:100), 5))
)
我怎样才能解决这个问题?
生成后,我们可以像编辑任何其他 ggplot
对象一样对其进行编辑。我们可以使用 scale_x_discrete()
来操纵轴标签,这避免了与 ggdotchart()
的原始绘图定义和构造的任何混淆。使用您的第一个情节 p
,我们可以:
alg_labels <- rep(paste( "Algorithm ", seq(1,5), sep=""), 5)
p +
scale_x_discrete(
labels = alg_labels
)