1

I have a little problem with a ggplot barchart.

I wanted to make a barchart with ggplot2 in order to compare my Svolumes for my 4 stocks on a period of few months.

I have two problems:

The first one is that my y axis is wrong. My graph/data seems correct but the y axis don't "follow" as I thought it will contain another scale... I would to have to "total" number of my dataset svolumes, I think here it is writing my svolumes values. I don't know how to explain but I would like the scale corresponding to all of my data on the graph like 10,20,etc until my highest sum of svolumes.

There is my code:

Date=c(rep(data$date))
Subject=c(rep(data$subject))
Svolume=c(data$svolume)
Data=data.frame(Date,Subject,Svolume)

Data=ddply(Data, .(Date),transform,pos=cumsum(as.numeric(Svolume))-(0.5*(as.numeric(Svolume))))

ggplot(Data, aes(x=Date, y=Svolume))+
  geom_bar(aes(fill=Subject),stat="identity")+
  geom_text(aes(label=Svolume,y=pos),size=3)

and there is my plot:

my pbarchart

I helped with the question here

Finally, How could I make the same plot for each months please? I don't know how to get the values per month in order to have a more readable barchart as we can't read anything here...

If you have other ideas for me I would be very glad to take any ideas and advices! Maybe the same with a line chart would be more readable...? Or maybe the same barchart for each stocks ? (I don't know how to get the values per stock either...)

I just found how to do it with lines.... but once again my y axis is wrong, and it's not very readable....

linechart

Thanks for your help !! :)

Community
  • 1
  • 1
Cec Jurczyk
  • 87
  • 1
  • 10
  • your y-axis variable is a factor. – user20650 Jul 16 '15 at 14:43
  • You could facet by month. – lawyeR Jul 16 '15 at 14:47
  • Oh yes thank you my Y axis was wrong because my volume were considered as character So I wrote: data$svolume=as.numeric(data$svolume) and now it's working.... Sorry... Yes but I don't know how to do it for each month, I don't know how to "reach" data per month – Cec Jurczyk Jul 16 '15 at 14:51
  • be careful when converting to numeric... if it is a factor just using `as.numeric` often leads to wrong values. Use `as.numeric(as.character(yourvar))`. But more importantly i would chack to see why a numeric variable is getting read in incorrectly. – user20650 Jul 16 '15 at 14:54

1 Answers1

2

Try adding the following line right before your ggplot function. It looks like your y-axis is in character.

[edit] Incorporate @user20650's comments, add as.character() first then convert to numeric.

Data$Svolume <- as.numeric(as.character(Data$Svolume))

To produce the same plot for each month, you can add the month variable first: Data$Month <- month(as.Date(Date)). Then add facet to your ggplot object.

ggplot(Data, aes(x=Date, y=Svolume) +
  ...
  + facet_wrap(~ Month)

For example, your bar chart code will be:

Data$Svolume <- as.numeric(as.character(Data$Svolume))
Data$Month <- month(as.Date(Date))
ggplot(Data, aes(x=Date, y=Svolume)) +
  geom_bar(aes(fill=Subject),stat="identity") +
  geom_text(aes(label=Svolume,y=pos),size=3) +
  facet_wrap(~ Month)

and your Line chart code will be:

Data$Svolume <- as.numeric(as.character(Data$Svolume))
Data$Month <- month(as.Date(Date))
ggplot(Data, aes(x=Date, y=Svolume, colour=Subject)) +
  geom_line() +
  facet_wrap(~ Month)
Boxuan
  • 4,937
  • 6
  • 37
  • 73
  • Thank you ! Yes I have the line chart per month with your code but I have 7 graphs for my seven month so 1, 2, 3... instead of "January,February"... Do you know How could I fix this? Besides on my x-axis I don't have the days, but I have "jan,feb,..." in Date I have yyyy-mm-dd, in datetime I have yyyy-mm-dd hh-mm-ss. How can I have the days in order to analyse per month the intraday values please? Thank you for your help – Cec Jurczyk Jul 16 '15 at 16:44
  • Like I have the result I wanted but it's too "far" as I still have on x-axis all the dates and not only my month. Like I would like to "zoom" to only have the data for my month and not my month with empty fields to the right/left depending on the month I have. Sorry for my english I hope you understand what I mean.. – Cec Jurczyk Jul 16 '15 at 17:00
  • @CecJurczyk Do you mind pasting what you have now in your original post? If you can have some code to generate a sample of your data, that will be so much easier. – Boxuan Jul 16 '15 at 18:53