使用ggplot散布点 [英] Using ggplot for scattering dots

查看:76
本文介绍了使用ggplot散布点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了以下数据集:

 Name     Year-Month     Value
 A         2002-01        -3.45
 A         2003-02         2.87
 A         2004-05         1.78
 A         2005-01        -9.54
 B         2000-01        -1.45
 B         2001-02         10.87
 B         2002-01         5.78
 C         2004-01        -6.45
 C         2005-01         4.87

我想要做的是我想以特殊方式绘制值. 在x轴上应该有年份和月份,但是由于在2000-2008年之间有很多年份和月份的观测值,我只想写出一年中的第一个月和第六个月,而一年中的其他所有月份都是只是用一个标志标记.

What I want to do is I want to plot the values in special way. On the x-Axis there should be the years and months, but as there are many observations from years and months between 2000-2008 I only want to write out the first and six month of the year and all the other months of the year are just marked by a sign.

对于所有观察,我都希望将值分散成点或十字形,而不管它们来自哪个字母.

For all observations I want to scatter the values like a dot or a cross, not matter from which letter they are.

我画了一张照片.

这张照片只是一个片段.在实际图中,值必须精确地在年月所在的行上.

This picture is just a scatch. In the real plot, the values have to be excatly on the line where the year-month is.

是否有使用ggplot2或其他任何软件包执行此操作的简便方法?

Is there an easy way of doing this with ggplot2 or any other package?

推荐答案

要每6个月显示一次x轴标签,同时每月显示次要刻度线,我们需要一些技巧:每个月进行一次主要刻度线,但只显示每6个月贴标签.

To display the x-axis labels every 6 months while showing the minor ticks every month, we need a little trick: make major ticks for every month but only show labels every 6 months.

要使用scale_x_date,需要从Year-Month创建一个伪" Date列.在这里,我只是将每月01的第一天添加到现有的Year-Month列中.

To make use of scale_x_date, creating a "fake" Date column from Year-Month is needed. Here I just append the first day of the month 01 to the existing Year-Month column.

library(magrittr)
library(tidyverse)

df <- read.table(text = "Name     Year-Month     Value
 A         2002-01        -3.45
 A         2003-02         2.87
 A         2004-05         1.78
 A         2005-01        -9.54
 B         2000-01        -1.45
 B         2001-02         10.87
 B         2002-01         5.78
 C         2004-01        -6.45
 C         2005-01         4.87",
                 header = TRUE)

# Create a Date column so that scale_x_date can be used
df %<>% 
  as.tibble() %>% 
  mutate(Date = as.Date(paste0(Year.Month, "-01")))
df

#> # A tibble: 9 x 4
#>   Name  Year.Month  Value Date      
#>   <fct> <fct>       <dbl> <date>    
#> 1 A     2002-01     -3.45 2002-01-01
#> 2 A     2003-02      2.87 2003-02-01
#> 3 A     2004-05      1.78 2004-05-01
#> 4 A     2005-01     -9.54 2005-01-01
#> 5 B     2000-01     -1.45 2000-01-01
#> 6 B     2001-02     10.9  2001-02-01
#> 7 B     2002-01      5.78 2002-01-01
#> 8 C     2004-01     -6.45 2004-01-01
#> 9 C     2005-01      4.87 2005-01-01

# Auto x-axis break
ggplot(df, aes(x = Date, y = Value)) +
  geom_point(pch = 4, size = 5) +
  scale_x_date(expand = c(0.015, 0.015),
               breaks = scales::pretty_breaks(), date_labels = "%Y-%b") +
  theme_bw()

# Break every 6 months
ggplot(df, aes(x = Date, y = Value)) +
  geom_point(pch = 4, size = 5) +
  scale_x_date(expand = c(0.015, 0.015),
               date_breaks = "6 months", date_labels = "%Y-%b") +
  theme_bw()

# Color by Name, manually setup date range
ggplot(df, aes(x = Date, y = Value, color = Name)) +
  geom_point(pch = 4, size = 5) +
  scale_x_date(expand = c(0.015, 0.015),
               breaks = seq(min(df$Date), max(df$Date), by = "6 months"), 
               date_minor_breaks = "1 month",
               date_labels = "%Y-%b") +
  theme_bw()

# Add minor tick
# Trick: make major ticks for every month but only show labels every 6 months
labels_month = format(seq(from = min(df$Date), to = max(df$Date), by = "1 months"), 
                      "%Y-%b")
labels_month[rep(c(FALSE, TRUE), c(1, 4))] <- ""
labels_month
#>  [1] "2000-Jan" ""         ""         ""         ""         "2000-Jun"
#>  [7] ""         ""         ""         ""         "2000-Nov" ""        
#> [13] ""         ""         ""         "2001-Apr" ""         ""        
#> [19] ""         ""         "2001-Sep" ""         ""         ""        
#> [25] ""         "2002-Feb" ""         ""         ""         ""        
#> [31] "2002-Jul" ""         ""         ""         ""         "2002-Dec"
#> [37] ""         ""         ""         ""         "2003-May" ""        
#> [43] ""         ""         ""         "2003-Oct" ""         ""        
#> [49] ""         ""         "2004-Mar" ""         ""         ""        
#> [55] ""         "2004-Aug" ""         ""         ""         ""        
#> [61] "2005-Jan"

x_breaks = seq(min(df$Date), max(df$Date), by = "1 months")

ggplot(df, aes(x = Date, y = Value, color = Name)) +
  geom_point(pch = 4, size = 5) +
  scale_x_date(expand = c(0.015, 0.015),
               labels = labels_month, 
               breaks = x_breaks) +
  theme_classic() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

reprex软件包(v0.2.0)创建于2018-06-05.

Created on 2018-06-05 by the reprex package (v0.2.0).

这篇关于使用ggplot散布点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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