Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在ggplot2中操纵x轴上的值时出现问题_R_Plot_Ggplot2 - Fatal编程技术网

R 在ggplot2中操纵x轴上的值时出现问题

R 在ggplot2中操纵x轴上的值时出现问题,r,plot,ggplot2,R,Plot,Ggplot2,我正在尝试用ggplot2创建一个直线图。该图是正确的,但x轴上的年份存在问题。我希望下面给出的数据框中的所有年份都显示在绘图中,但只有年份列中的少数值显示在绘图中。有什么解决办法吗? 数据如下: 去年袭击事件死亡 1999 1 2 2000 1 1 2001 3 3 2002 1 1 2003 2 65 2004 1 1 2005 1 1 2006 1 1 2007 1 1 2008 1 1

我正在尝试用ggplot2创建一个直线图。该图是正确的,但x轴上的年份存在问题。我希望下面给出的数据框中的所有年份都显示在绘图中,但只有年份列中的少数值显示在绘图中。有什么解决办法吗? 数据如下:

去年袭击事件死亡
1999    1   2
2000    1   1
2001    3   3
2002    1   1
2003    2   65
2004    1   1
2005    1   1
2006    1   1
2007    1   1
2008    1   1
2009    1   1
2010    4   75
2011    4   61
2012    9   32
2013    2   180
2014    4   70
2015    4   13
2016    3   7
2017    9   25
绘制图的代码如下所示:

data <- read.table("data.txt", header = T)
p <- ggplot(moomins,aes(year))+
+ geom_line(aes(y= attacks,color = "Attacks"))+
+ geom_line(aes(y= killed,color = "Killed"))
p + coord_flip(xlim = c(1999,2017 ))
print(p)

data您可以在ggplot中添加以下内容:(我认为您在命名方面不一致--
moomin
是您的数据帧名称,对吗?)

如果这对你不起作用,请告诉我。除非打印尺寸非常大,否则可能需要旋转记号标签,因为标签将开始重叠

考虑你的年龄

fyear<-factor(year)
fyear试试这个:

df %>% 
  ggplot(aes(year)) +
  geom_line(aes(y = attacks, color = "Attacks")) +
  geom_line(aes(y = killed, color = "Killed")) +
  scale_x_continuous(breaks = seq(min(df$year), 
                                  max(df$year)))
其中:

我使用了以下数据:

df <- c("
        year    attacks killed
        1999    1   2
        2000    1   1
        2001    3   3
        2002    1   1
        2003    2   65
        2004    1   1
        2005    1   1
        2006    1   1
        2007    1   1
        2008    1   1
        2009    1   1
        2010    4   75
        2011    4   61
        2012    9   32
        2013    2   180
        2014    4   70
        2015    4   13
        2016    3   7
        2017    9   25
        ")
df <- read.table(text = df, header = TRUE)
df <- as_tibble(df)

df您的数据很难用这种格式复制代码。您最好提供
dput()的输出
进入问题。我猜如果你在
scale\u x\u continuous
参数中包含
breaks=1999:2017
,你应该修正它:我检查了你的解决方案,但它没有正常工作,但感谢你的帮助。你的解决方案产生了与我在顶部绘制的相同的图,但感谢你的帮助。
df %>% 
  ggplot(aes(year)) +
  geom_line(aes(y = attacks, color = "Attacks")) +
  geom_line(aes(y = killed, color = "Killed")) +
  scale_x_continuous(breaks = seq(min(df$year), 
                                  max(df$year)))
df <- c("
        year    attacks killed
        1999    1   2
        2000    1   1
        2001    3   3
        2002    1   1
        2003    2   65
        2004    1   1
        2005    1   1
        2006    1   1
        2007    1   1
        2008    1   1
        2009    1   1
        2010    4   75
        2011    4   61
        2012    9   32
        2013    2   180
        2014    4   70
        2015    4   13
        2016    3   7
        2017    9   25
        ")
df <- read.table(text = df, header = TRUE)
df <- as_tibble(df)