简体   繁体   English

如何翻转 ggplot2 图表上的趋势线模式?

[英]How do I flip the trendline patterns on my ggplot2 graph?

I want to make the Girls have the dashed trendline and the Boys have a solid trendline.我想让女孩有虚线趋势线,而男孩有实线趋势线。 I'd also like to remove the box around the graph, save the y and x-axis lines, and the shading behind the shapes on the key.我还想删除图形周围的框,保存 y 和 x 轴线,以及键上形状后面的阴影。 I am using ggplot2 in R.我在 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)

Current Graph当前图表

There is quite something going on in your visualisation.你的可视化中发生了很多事情。 One strategy to develop this is to add layer and feature by feature once you have your base plot.开发此功能的一种策略是在您拥有基础 plot 后逐个功能添加层和功能。

There a different ways to change the "sequence" of your colours, shapes, etc. You can do this in ggplot with one of the scale_xxx_manual layers.有不同的方法可以更改颜色、形状等的“顺序”。您可以在 ggplot 中使用 scale_xxx_manual 图层之一来执行此操作。

Conceptually, I suggest you deal with this in the data and only use the scales for "twisting".从概念上讲,我建议你在数据中处理这个问题,并且只使用尺度来“扭曲”。 But that is a question of style.但这是风格问题。
In your case, you use Sex1M as a categorical variable.在您的情况下,您使用Sex1M作为分类变量。 There is a built in sequence for (automatic) colouring and shapes.有一个用于(自动)着色和形状的内置序列。 So in your case, you have to "define" the levels in another order.因此,在您的情况下,您必须以另一个顺序“定义”级别。

As you have not provided a representative sample, I simulate some data points and define Sex1M as part of the data creation process.由于您没有提供具有代表性的样本,我模拟了一些数据点并将 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 )

Now let's plot现在让我们 plot

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)

The settings for the background panel make the outer line disappear in my plot.背景面板的设置使外线在我的 plot 中消失。 Hope this helps to get you started.希望这有助于您入门。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM