重新排列X轴类别并使用ggplot2分隔图例

时间:2018-11-09 17:55:16

标签: r ggplot2

使用R和ggplot2的新手,请原谅我的代码的新颖性。
我正在尝试保留csv文件中x轴的顺序,但是按字母顺序对其进行了重新排序。
我尝试使用scale_x_discrete,但是我认为我的实现不佳,或者可能不合适。
结果是重新排序的x轴,但绘制的点按字母顺序保留。此外,我的传说还包含了加权平均数。我想拥有一个定义红色X的独立的第三个图例。如何分离相同类型的geom层?

#Main plot
myplot <- ggplot(mydata, aes(x = treatment, y = delta_ases)) +              #plot treatment(x) vs score(y)
                 geom_point(aes(size = sample_size)) +                      #adjust point area by sample size
                 geom_point(aes(y=ases_weighted_avg, size=30),    
                        color="red", shape=4) +                             #plot weighted average, show.legend = FALSE?
                 scale_y_continuous(breaks = seq(0, 100, by = 10), 
                                limits = c(5, 75)) +                        #cleaner y-axis
                 theme_bw() + 
                 theme(panel.border = element_blank(), 
                       panel.grid.major = element_blank(),
                       panel.grid.minor = element_blank(), 
                       axis.line = element_line(colour = "black"))          #remove background and grid elements

myplot <- myplot +  labs(x = "Treatment Strategy") +                        #rename x-axis
                    labs(y = "Change in ASES Score") +                      #rename y-axis
                    labs(size = "Sample Size") +                            #rename sample size legend
                    labs(linetype = "MCID") +                               #rename line legend
                    geom_hline(aes(yintercept = 17, linetype = "PT"), 
                               color = 'black') +                           #create horizontal line for Physical Therapy Group
                    geom_hline(aes(yintercept = 39, linetype = "Surgery"), 
                               color = 'black')                             #create horizontal line for Surgical Group

myplot

Plotted output

样本数据

structure(list(treatment = structure(c(5L, 5L, 5L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 7L, 7L, 1L, 1L, 6L, 6L, 6L, 
6L, 6L, 6L), .Label = c("BA", "D", "GI", "PR", "PT", "RSA", "SCR", 
"TT"), class = "factor"), sample_size = c(30L, 45L, 19L, 33L, 
23L, 41L, 33L, 57L, 31L, 38L, 13L, 90L, 37L, 22L, 28L, 11L, 25L, 
5L, 21L, 41L, 27L, 14L, 86L, 33L, 55L, 67L, 55L, 52L, 15L, 60L, 
42L, 36L, 23L, 5L, 20L, 64L, 17L, 58L, 25L, 49L, 34L), delta_ases = c(23, 
NA, NA, 28, NA, NA, NA, NA, 45.8, NA, NA, NA, 32.6, NA, 32.7, 
NA, 39.1, 45, NA, NA, NA, NA, 40.1, NA, NA, NA, NA, NA, NA, NA, 
24.9, 35.8, 69.4, NA, NA, 32.4, NA, 42.1, NA, NA, NA), delta_cms = c(NA, 
13, NA, 21, 31.4, 28, 30, NA, 16.8, NA, 27.7, 37.2, NA, 30.9, 
NA, NA, NA, NA, 37.8, 46.4, 34, 26, 25.9, NA, 16.5, 14, 28.4, 
36, 38.7, 28.5, 27.4, NA, NA, 25, 33.2, NA, NA, NA, 28.4, 36.7, 
35.6), ases_weighted_avg = c(23, NA, NA, 36.621875, NA, NA, NA, 
NA, NA, NA, 34.43666667, NA, NA, NA, NA, NA, NA, 45, NA, NA, 
35.1125, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 48.89830508, 
NA, NA, NA, 37.01147541, NA, NA, NA, NA, NA), cms_weighted_avg = c(NA, 
13, NA, 25.30434783, NA, NA, NA, NA, NA, NA, 35.1032, NA, NA, 
NA, NA, NA, NA, 43.48709677, NA, NA, 25.85665962, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 31.56, NA, 34.43240741, NA, 
NA, NA, NA, NA)), class = "data.frame", row.names = c(NA, -41L
))

1 个答案:

答案 0 :(得分:0)

您必须分别定义该形状。

d <- ggplot(mydata, aes(x = factor(treatment, levels = c("PT", "D", "PR", "GI")), y = delta_ases, size = sample_size)) +
  geom_point()

d + geom_point(aes(y = ases_weighted_avg, shape = "mean"), geom = "point", size = 6, color = "red") +
  guides(size = guide_legend(order = 1)) + scale_shape_manual("", values = c("mean" = "x"))

添加主题和标签。