ggplot2 |仅针对一个变量自定义geom_point [英] ggplot2 | Customize geom_point for only one variable

查看:80
本文介绍了ggplot2 |仅针对一个变量自定义geom_point的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据所附的情节,我相当有能力实现应有的目标.但是,我坚持进行必要的基本更改.

As per the attached plot, I was fairly able to achieve what I had ought to. However, I'm stuck at an elementary change that is necessary.

目标:

  1. 仅允许 geom_point value1 ,其余变量将为虚线.
  2. 值1 应在图例中代表一个,而不是空白.
  1. allow geom_point to only value1 and the rest of the variables will be dashed lines.
  2. Value1 should represent a point in the legend instead of the blank.


library(ggplot2)
library(tidyverse)
library(reshape2)

#Creating a dataframe with use-case specific variables. 
df = data.frame(
                Year = 2006:2025,

                Value1 = c(40.5, 39.0, NA, NA, NA, NA, 29.9, NA, NA, NA, 21.6,
                             NA, NA, NA, NA, NA, NA, NA, NA, NA),

                Value2 = c(NA, NA, NA, NA, NA, NA, 29.9, NA, NA, NA, NA,
                             NA, NA, NA, NA, NA, NA, NA, NA, 14.9),

                Value3 = c(NA, NA, NA, NA, NA, NA, 29.9, NA, NA, NA, NA,
                             NA, NA, NA, NA, NA, NA, NA, NA, 13.0),

                Value4 = c(NA, NA, NA, NA, NA, NA, 29.9, 27.6, 25.4, 23.4, 21.6,
                             19.9, 18.4, 16.9, 15.6, 14.4, 13.3, NA, 12.2, 11.3)
                  )

#Transforming data
df <- melt(df,id.vars = "Year")

#Creating a line plot (Year vs. other variables)
ggplot(df[!is.na(df$value),], aes(x=factor(Year), y=value, group=variable)) +
  geom_line(aes(linetype=variable, color=variable, size=variable), na.rm = TRUE )+
  geom_point(na.rm = TRUE)+
  scale_linetype_manual(values=c("blank","dashed","dashed","dashed"))+
  scale_color_manual(values=c('#999999','orange2','turquoise2','blue2'))+
  scale_size_manual(values=c(0, 1.5, 1.5, 1.5))+
  theme(legend.position="top")+
  
scale_y_continuous(
breaks=seq(0,100, 10), labels = seq(0, 100, 10), limits=c(0,70),
sec.axis = sec_axis(
  name='measure (%)', trans='identity',
  breaks=seq(0,100,10), labels=seq(0,100,10))) +
  
theme(
legend.position = 'bottom', legend.direction = 'horizontal',
panel.grid.major.y = element_line(color='gray85'),
axis.title = element_text(face='bold')) +
  
labs(
x='Year', y='measure (%)')

 Created on 2020-07-10 by the reprex package (v0.3.0)

 code contributors: chemdork123, pradeepvaranasi


输出图:

尽管我可以自定义geom_points的形状和颜色,但我找不到能够实现目标的正确资源.但是,这将创建一个额外的图例.

I couldn't find the right resources that'll achieve the objectives though I can customize the shapes and colours of the geom_points. However, that will create an additional legend.

任何建议都会有所帮助.

Any suggestions will be helpful.

推荐答案

您可以单独定义geom_point的数据,使其仅包含value1.这就是你想要的吗?

You can define the data for the geom_point separately, so that it only contains value1. Is this what you want?

ggplot(df[!is.na(df$value),], aes(x=factor(Year), y=value, group=variable)) +
  geom_line(aes(linetype=variable, color=variable, size=variable), na.rm = TRUE )+
  geom_point(data = df[!is.na(df$value) & df$variable == "Value1",], aes(color = variable), na.rm = TRUE) +
  scale_linetype_manual(values=c("blank","dashed","dashed","dashed"))+
  scale_color_manual(values=c('#999999','orange2','turquoise2','blue2'))+
  scale_size_manual(values=c(0, 1.5, 1.5, 1.5))+
  theme(legend.position="top")+
  
  scale_y_continuous(
    breaks=seq(0,100, 10), labels = seq(0, 100, 10), limits=c(0,70),
    sec.axis = sec_axis(
      name='measure (%)', trans='identity',
      breaks=seq(0,100,10), labels=seq(0,100,10))) +
  
  theme(
    legend.position = 'bottom', legend.direction = 'horizontal',
    panel.grid.major.y = element_line(color='gray85'),
    axis.title = element_text(face='bold')) +
  
  labs(x='Year', y='measure (%)')

我编辑了绘图,以便可以直接定义点的大小(并将它们从图例中删除为带线的值):

I edited the plot so the size of the points can be defined directly (and they are removed from the legend for the values with lines):

ggplot(data = NULL, aes(x=factor(Year), y=value, group=variable)) +
  geom_line(data = df[!is.na(df$value) & df$variable != "Value1",],
            aes(linetype=variable, color = variable), size = 1.5, linetype = "dashed")+
  geom_point(data = df[!is.na(df$value) & df$variable == "Value1",], 
             aes(color = variable), size = 4) +
  scale_color_manual(values=c('#999999', 'orange2','turquoise2','blue2'))+
  guides(color = guide_legend(override.aes = list(linetype = c("blank", "dashed", "dashed", "dashed"),
                                                  shape = c(16, NA, NA, NA)))) +
  scale_y_continuous(
    breaks=seq(0,100, 10), labels = seq(0, 100, 10), limits=c(0,70),
    sec.axis = dup_axis()) +
  theme(
    legend.position = 'bottom', legend.direction = 'horizontal',
    panel.grid.major.y = element_line(color='gray85'),
    axis.title = element_text(face='bold')) +
  
  labs(x='Year', y='measure (%)')

这篇关于ggplot2 |仅针对一个变量自定义geom_point的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆