首页 > 解决方案 > 如何翻转 ggplot2 图表上的趋势线模式?

问题描述

我想让女孩有虚线趋势线,而男孩有实线趋势线。我还想删除图形周围的框,保存 y 和 x 轴线,以及键上形状后面的阴影。我在 R 中使用 ggplot2。

dr <- ggplot(DATASET, 
             aes(x=EC, 
                  y=sqrt_Percent.5, 
                  color=Sex1M, 
                  shape=Sex1M, 
                  linetype=Sex1M)) + 
    geom_point(size= 3, 
               aes(shape=Sex1M,                                                    
               color=Sex1M)) + 
    scale_shape_manual(values=c(1,16))+
    geom_smooth(method=lm,
                se=FALSE,
                fullrange=TRUE) + 
    labs(x="xaxis title", 
        y = "yaxis title", 
        fill= "") + 
     xlim(3,7) + 
     ylim(0,10)  + 
    theme(legend.position = 'right', 
          legend.title = element_blank(),
          panel.border = element_rect(fill=NA, 
                                      color = 'white'),   
                                      panel.background = NULL,
                                      legend.background =element_rect(fill=NA,
              size=0.5,
              linetype="solid")) + 
scale_color_grey(start = 0.0,
                 end = 0.4)

当前图表

标签: rggplot2graphdata-visualizationgraph-visualization

解决方案


你的可视化中发生了很多事情。开发此功能的一种策略是在您拥有基础图后逐个添加层和特征。

有不同的方法可以更改颜色、形状等的“顺序”。您可以在 ggplot 中使用 scale_xxx_manual 图层之一进行此操作。

从概念上讲,我建议你在数据中处理这个问题,并且只使用尺度来“扭曲”。但这是风格问题。
在您的情况下,您将Sex1M其用作分类变量。有一个用于(自动)着色和形状的内置序列。因此,在您的情况下,您必须以另一个顺序“定义”级别。

由于您没有提供具有代表性的样本,我模拟了一些数据点并将 Sex1M 定义为数据创建过程的一部分。

DATASET <- data.frame(
     x = sample(x = 2:7, size = 20, replace = TRUE)
   , y = sample(x = 0.2:9.8, size = 20, replace = TRUE)
   , Sex1M = sample(c("Boys", "Girls"), size = 20, replace = TRUE )

现在让我们绘制

library(dplyr)
library(ggplot2)

DATASET <- DATASET %>% 
   mutate(Sex1M = factor(Sex1M, levels = c("Boys","Girls))  # set sequence of levels: boys are now the first level aka 1st colour, linetype, shape.

# plot
ggplot(DATASET, 
       aes(x=x,    # adapted to simulated data 
           y=y,    # adapted to simulated data
           color=Sex1M,    # these values are now defined in the sequence
           shape=Sex1M,    # of the categorical factor you created
           linetype=Sex1M) # adapt the factor levels as needed (e.g change order)
    ) + 
    geom_point(size= 3, 
               aes(shape=Sex1M,                                                    
                   color=Sex1M)) + 
    scale_shape_manual(values=c(1,16))+
    geom_smooth(method=lm,
                se=FALSE,
                fullrange=TRUE) + 
    labs(x="xaxis title", 
         y = "yaxis title", 
         fill= "") + 
    xlim(3,7) + 
    ylim(0,10)  + 
    theme(legend.position = 'right', 
          legend.title = element_blank(),
          panel.border = element_rect(fill=NA, 
                                      color = 'white'),   
          panel.background = NULL,
#------------ ggplot is not always intuitive - the legend background the panel 
# comprising the legend keys (symbols) and the labels
# you want to remove the colouring of the legend keys
          legend.key = element_rect(fill = NA),
# ----------- that can go. To see above mentioned difference of background and key
# set fill = "blue"
          # legend.background =element_rect(fill = NA, size=0.5,linetype="solid")
          ) + 
    scale_color_grey(start = 0.0,
                     end = 0.4)

背景面板的设置使外线在我的情节中消失了。希望这有助于您入门。

在此处输入图像描述


推荐阅读