ggplot

我试图绘制我的数据集婚姻,该婚姻由州,年份和比率组成。但是,我尝试着眼于特定的年份间隔,以使图表看起来不太拥挤。

marriage<-read.csv(file="~/Desktop/datah.csv",header=T,sep=",",check.names=FALSE)
marriage
marriage <- marriage %>%
     gather(key=year,value=rate,`2017`:`1990`)
ggplot(marriage,aes(x=year,y=rate,group=State)) +
     geom_point(aes(color=State)) +
     geom_line(aes(color=State)) +
     theme_bw()

我尝试添加以下内容,以尝试将x和y轴限制为上述代码的最后一行

+ylim(0,2)
+scale_x_continuous(limits=c(2000,2005))
+xlim(2010,2015)
+scale_x_continous(breaks = seq(2000,2005,5))

但是我明白了,错误:离散值提供给连续刻度

我还试图将其转换为数字

marriage$variable=as.numeric(levels(marriage$variable))[marriage$variable]

我收到以下错误 $

这是数据的前几行

                  State 2017 2016 2015 2014 2013 2012 2011 2010 2009 2008 2007 2006 2005 2004 2003 2002 2001 2000 1999 1995 1990
1               Alabama  7.0  7.1  7.4  7.8  7.8  8.2  8.4  8.2  8.3  8.6  8.9  9.2  9.2  9.4  9.6  9.9  9.4 10.1 10.8  9.8 10.6
2                Alaska  6.9  7.1  7.4  7.5  7.3  7.2  7.8  8.0  7.8  8.4  8.5  8.2  8.2  8.5  8.1  8.3  8.1  8.9  8.6  9.0 10.2
3               Arizona  5.8  5.9  5.9  5.8  5.4  5.6  5.7  5.9  5.6  6.0  6.4  6.5  6.6  6.7  6.5  6.7  7.6  7.5  8.2  8.8 10.0
4              Arkansas  9.5  9.9 10.0 10.1  9.8 10.9 10.4 10.8 10.7 10.6 12.0 12.4 12.9 13.4 13.4 14.3 14.3 15.4 14.8 14.4 15.3
5           California   6.3  6.5  6.2  6.4  6.5  6.0  5.8  5.8  5.8  6.7  6.2  6.3  6.4  6.4  6.1  6.2  6.5  5.8  6.4  6.3  7.9
6              Colorado  7.3  7.4  6.8  7.1  6.5  6.8  7.0  6.9  6.9  7.4  7.1  7.2  7.6  7.4  7.8    8  8.2  8.3  8.2  9.0  9.8
7           Connecticut  5.6  5.6  5.3  5.4    5  5.2  5.5  5.6  5.9  5.4  5.5  5.5  5.8  5.8  5.5  5.7  5.4  5.7  5.8  6.6  7.9
8              Delaware  5.5  5.6  5.7    6  6.6  5.8  5.2  5.2  5.4  5.5  5.7  5.9  5.9  6.1    6  6.4  6.5  6.5  6.7  7.3  8.4
9  District of Columbia  8.2  8.1  8.2 11.8 10.8  8.4  8.7  7.6  4.7  4.1  4.2    4  4.1  5.2  5.1  5.1  6.2  4.9  6.6  6.1  8.2
10              Florida  7.8  8.1  8.2  7.3    7  7.2  7.4  7.3  7.5  8.0  8.5  8.6  8.9  9.0    9  9.4  9.3  8.9  8.7  9.9 10.9
11              Georgia  6.9  6.8  6.2  ---  ---  6.5  6.6  7.3  6.6  6.0  6.8  7.3  7.0  7.9    7  6.5  6.1  6.8  7.8  8.4 10.3
12               Hawaii 15.3 15.6 15.9 17.7 16.3 17.5 17.6 17.6 17.2 19.1 20.8 21.9 22.6 22.6   22 20.8 19.6 20.6 18.9 15.7 16.4
13                Idaho  7.8  8.1  8.2  8.4  8.2  8.2  8.6  8.8  8.9  9.5 10.0 10.1 10.5 10.8 10.9   11 11.2 10.8 12.1 13.1 13.9
zgwl369 回答:ggplot

尝试

scale_x_continous(breaks = seq(2000,2015,5)

对不起,您的评论不足够

,

我认为ggplot()实际上会以体面的间隔自动完成体面的工作。如果您想让它自己做,请尝试将年份转换为日期。一种简单的方法是使用make_date()中的lubridate

图书馆

library(dplyr)
library(tidyr)
library(lubridate)
library(ggplot2)

读入数据(根据问题更新进行编辑)

这是基于编辑的数据。这应该像您所拥有的那样将其引入。请注意,我显然使用的方法与您不同。

我认为您需要像往常一样添加na.strings = "---",以便NA字段可以正确读取。

此外,我必须在单引号内添加哥伦比亚特区。这可能是您遇到的问题。

data <- "State 2017 2016 2015 2014 2013 2012 2011 2010 2009 2008 2007 2006 2005 2004 2003 2002 2001 2000 1999 1995 1990
Alabama  7.0  7.1  7.4  7.8  7.8  8.2  8.4  8.2  8.3  8.6  8.9  9.2  9.2  9.4  9.6  9.9  9.4 10.1 10.8  9.8 10.6
Alaska  6.9  7.1  7.4  7.5  7.3  7.2  7.8  8.0  7.8  8.4  8.5  8.2  8.2  8.5  8.1  8.3  8.1  8.9  8.6  9.0 10.2
Arizona  5.8  5.9  5.9  5.8  5.4  5.6  5.7  5.9  5.6  6.0  6.4  6.5  6.6  6.7  6.5  6.7  7.6  7.5  8.2  8.8 10.0
Arkansas  9.5  9.9 10.0 10.1  9.8 10.9 10.4 10.8 10.7 10.6 12.0 12.4 12.9 13.4 13.4 14.3 14.3 15.4 14.8 14.4 15.3
California   6.3  6.5  6.2  6.4  6.5  6.0  5.8  5.8  5.8  6.7  6.2  6.3  6.4  6.4  6.1  6.2  6.5  5.8  6.4  6.3  7.9
Colorado  7.3  7.4  6.8  7.1  6.5  6.8  7.0  6.9  6.9  7.4  7.1  7.2  7.6  7.4  7.8    8  8.2  8.3  8.2  9.0  9.8
Connecticut  5.6  5.6  5.3  5.4    5  5.2  5.5  5.6  5.9  5.4  5.5  5.5  5.8  5.8  5.5  5.7  5.4  5.7  5.8  6.6  7.9
Delaware  5.5  5.6  5.7    6  6.6  5.8  5.2  5.2  5.4  5.5  5.7  5.9  5.9  6.1    6  6.4  6.5  6.5  6.7  7.3  8.4
'District of Columbia'  8.2  8.1  8.2 11.8 10.8  8.4  8.7  7.6  4.7  4.1  4.2    4  4.1  5.2  5.1  5.1  6.2  4.9  6.6  6.1  8.2
Florida  7.8  8.1  8.2  7.3    7  7.2  7.4  7.3  7.5  8.0  8.5  8.6  8.9  9.0    9  9.4  9.3  8.9  8.7  9.9 10.9
Georgia  6.9  6.8  6.2  ---  ---  6.5  6.6  7.3  6.6  6.0  6.8  7.3  7.0  7.9    7  6.5  6.1  6.8  7.8  8.4 10.3
Hawaii 15.3 15.6 15.9 17.7 16.3 17.5 17.6 17.6 17.2 19.1 20.8 21.9 22.6 22.6   22 20.8 19.6 20.6 18.9 15.7 16.4
Idaho  7.8  8.1  8.2  8.4  8.2  8.2  8.6  8.8  8.9  9.5 10.0 10.1 10.5 10.8 10.9   11 11.2 10.8 12.1 13.1 13.9"

marriage <- read.table(textConnection(data),header = TRUE,na.strings = "---") %>%
  as_tibble() %>%
  rename_all(~ sub("X","",.))

此时,marriage应该是您的数据。我将稍作修改以转换为日期。

marriage <- marriage %>%
  gather(key=year,value=rate,`2017`:`1990`)%>%
  mutate(year = make_date(year))

情节

不更改绘图代码。轴是自然处理的。

ggplot(marriage,aes(x=year,y=rate,group=State)) +
  geom_point(aes(color=State)) +
  geom_line(aes(color=State)) +
  theme_bw()

graph output

,

变量'year'被存储为gather之后的字符。您可以调整重塑形状(更新为pivot_longer):

    marriage <-
      marriage %>%
      pivot_longer(
        cols = `2017`:`1990`,names_to = 'year',values_to = 'rate'
      ) %>%
      mutate(
        year = as.numeric(year)
      )

ggplot调用应从那里开始。

本文链接:https://www.f2er.com/3113718.html

大家都在问