简体   繁体   中英

How ask R not to combine the X axis values for a bar chart?

I am a beginner with R . My data looks like this:

id  count date
1   210 2009.01
2   400 2009.02
3   463 2009.03
4   465 2009.04
5   509 2009.05
6   861 2009.06
7   872 2009.07
8   886 2009.08
9   725 2009.09
10  687 2009.10
11  762 2009.11
12  748 2009.12
13  678 2010.01
14  699 2010.02
15  860 2010.03
16  708 2010.04
17  709 2010.05
18  770 2010.06
19  784 2010.07
20  694 2010.08
21  669 2010.09
22  689 2010.10
23  568 2010.11
24  584 2010.12
25  592 2011.01
26  548 2011.02
27  683 2011.03
28  675 2011.04
29  824 2011.05
30  637 2011.06
31  700 2011.07
32  724 2011.08
33  629 2011.09
34  446 2011.10
35  458 2011.11
36  421 2011.12
37  459 2012.01
38  256 2012.02
39  341 2012.03
40  284 2012.04
41  321 2012.05
42  404 2012.06
43  418 2012.07
44  520 2012.08
45  546 2012.09
46  548 2012.10
47  781 2012.11
48  704 2012.12
49  765 2013.01
50  571 2013.02
51  371 2013.03

I would like to make a bar graph like graph that shows how much what is the count for each date (dates in format of Month-Y, Jan-2009 for instance). I have two issues: 1- I cannot find a good format for a bar-char like graph like that 2- I want all of my data-points to be present in X axis(date), while R aggregates it to each year only (so I inly have four data-points there). Below is the current command that I am using:

plot(df$date,df$domain_count,col="red",type="h")

and my current plot is like this: 在此处输入图片说明

Ok, I see some issues in your original data. May I suggest the following:

Add the days in your date column

df$date=paste(df$date,'.01',sep='')

Convert the date column to be of date type:

df$date=as.Date(df$date,format='%Y.%m.%d')

Plot the data again:

plot(df$date,df$domain_count,col="red",type="h")

Also, may I add one more suggestion, have you used ggplot for ploting chart? I think you will find it much easier and resulting in better looking charts. Your example could be visualized like this:

library(ggplot2) #if you don't have the package, run install.packages('ggplot2')
ggplot(df,aes(date, count))+geom_bar(stat='identity')+labs(x="Date", y="Count")

First, you should transform your date column in a real date:

library(plyr) # for mutate
d <- mutate(d, month = as.numeric(gsub("[0-9]*\\.([0-9]*)", "\\1", as.character(date))),
               year  = as.numeric(gsub("([0-9]*)\\.[0-9]*", "\\1", as.character(date))),
               Date  = ISOdate(year, month, 1))

Then, you could use ggplot to create a decent barchart:

library(ggplot2)
ggplot(d, aes(x = Date, y = count)) + geom_bar(fill = "red", stat = "identity")

在此处输入图片说明

You can also use basic R to create a barchart, which is however less nice:

dd <- setNames(d$count, format(d$Date, "%m-%Y"))
barplot(dd)

在此处输入图片说明

The former plot shows you the "holes" in your data, ie month where there is no count, while for the latter it is even wuite difficult to see which bar corresponds to which month (this could however be tweaked I assume).

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM