2017-04-11 9 views
1

Ich versuche, die X-Achse meines Histogramms von 2000 bis 2016 um 1 zu zählen. Jetzt, wie im Bild gezeigt, zählt es um 5. Ich habe Folgendes versucht Erklärung ohne Erfolg:R Histogramm-Änderung X-Achsen-Inkrementalwert

axis(side=1, at=seq(1999,2017, 1))) 

Jede Hilfe dieses Problems viel

df <- read.table(textConnection(
    'Year Count 
    2016 1 
    2015 7 
    2014 8 
    2013 3 
    2012 13 
    2011 3 
    2010 6 
    2009 2 
    2008 2 
    2007 3 
    2006 1 
    2005 0 
    2004 1 
    2003 7 
    2002 1 
    2001 3 
    2000 0'), header = TRUE) 

hw <- theme_gray()+ theme(
    plot.title=element_text(hjust=0.5,face='bold',size=16), 
    axis.title.y=element_text(angle=0,vjust=.5,face='bold',size=13), 
    axis.title.x=element_text(face='bold',size=13), 
    plot.subtitle=element_text(hjust=0.5), 
    plot.caption=element_text(hjust=-.5), 

    strip.text.y = element_blank(), 
    strip.background=element_rect(fill=rgb(.9,.95,1), 
           colour=gray(.5), size=.2), 

    panel.border=element_rect(fill=FALSE,colour=gray(.70)), 
    panel.grid.minor.y = element_blank(), 
    panel.grid.minor.x = element_blank(), 
    panel.spacing.x = unit(0.10,"cm"), 
    panel.spacing.y = unit(0.05,"cm"), 

    axis.ticks=element_blank(), 
    axis.text=element_text(colour="black"), 
    axis.text.y=element_text(margin=margin(0,3,0,3),face="bold",size=10), 
    axis.text.x=element_text(margin=margin(-1,0,3,0),face="bold",size=10) 
) 




dfnew=NULL 
for (row in 1:nrow(df)) 
{ 
    temp=rep(df[row,]$Year,df[row,]$Count) 
    dfnew = rbind(dfnew,data.frame(Year=temp)) 
} 
df=dfnew 


library(ggplot2) 


ggplot(df,aes(x=Year)) + 
    geom_histogram()+ 
    labs(x="Year", 
     y="Count", 
     title="Year vs MLB No-Hitters Count")+hw 


ggplot(df,aes(x=Year)) + 
    geom_histogram(binwidth=1, 
       fill="cornsilk",color="black")+ 
    labs(x="Year", 
     y="Count", 
     title="Year vs MLB No-Hitters Count")+hw 


ggplot(df,aes(x=Year,..density..)) + 
    geom_histogram(binwidth=1, 
       fill="cornsilk",color="black")+ 
    labs(x="Year", 
     y="Count", 
     title="Year vs MLB No-Hitters Count")+hw 




histPlot <- ggplot(df,aes(x=Year,..density..))+ 
    geom_histogram(binwidth=1, fill="cornsilk",color="black")+ 
    labs(x="Year", 
     y="Density", 
     title="Year vs MLB No-Hitters Count")+hw 


histPlot 


histPlot + geom_freqpoly(binwidth=1,color="red",size=1.2) 


histPlot + geom_line(stat="density",color="blue",size=1.2)+ 
    xlim(1999,2017) 


histPlot + 
    geom_density(adjust=.4,fill="cyan",color="black",alpha=.40)+ 
    xlim(1999,2017) 

enter image description here

+0

Und ich erkannte gerade das Bereichsproblem war ein Tippfehler. –

Antwort

2

Eine weitere ggplot Option würde geschätzt. Sie müssen den Text jedoch um 90 Grad drehen.

+ scale_x_continuous(breaks=c(2000:2016)) 
+0

Danke! Ich habe am Ende meines Programms eingegeben, was Sie gesagt haben, und den Text nicht um 90 Grad gedreht, sondern die gewünschte Ausgabe erhalten. –