0

I am a student programmer attempting to plot a histogram.

I have following sample histogram data.

V1  V2
214 6
215 6
216 6
217 5
218 5
219 6
220 5
221 6
222 6
223 6
224 6
225 6
226 6
227 7
228 7
229 7
230 7
231 8
232 8
233 8
234 8
235 8

The first column being what number is repeated and the second is the amount of repeats.

Currently, I am trying ggplot(df, aes(V1, V2)) + geom_bar() and I am not producing a graph.

I am probably overlooking an option. How would you plot this histogram?

Thank you

Nicholas Hayden
  • 473
  • 1
  • 3
  • 24

2 Answers2

1

Just pass the name of your variable into this code.

 hist(VARIABLE_NAME, 
      main="Histogram of XYZ", 
      xlab="X access", 
      border="blue", 
      col="green",
      xlim=c(100,700),
      las=1, 
      breaks=5)
Lowpar
  • 897
  • 10
  • 31
  • I am using a data frame with two variables. I am trying to make it with two vectors – Nicholas Hayden Jul 14 '16 at 20:29
  • 1
    I was wondering about that, I thought that the first vector was the row number because it descends in value. Do you have the prior code? I think where you have aes it should be factor and you should have 1 - ggplot(df, aes(x = factor(V1))) + geom_bar(stat = "bin") – Lowpar Jul 14 '16 at 20:37
  • Unfortunately I do not have the code for this data. I just updated the data to make it more clear. How would you feed V2 ( the frequency to the plot)? – Nicholas Hayden Jul 14 '16 at 20:39
  • I am getting the error Error in .Call.graphics(C_palette2, .Call(C_palette2, NULL)) : invalid graphics state – Nicholas Hayden Jul 14 '16 at 20:44
  • 1
    what about ggplot(df, aes(x=(V1), y=V2)) + geom_bar(stat = "identity") – Lowpar Jul 14 '16 at 20:47
  • That works! Is there a way to bin the data? I think I am overplotting (I have >600000 bars now ) – Nicholas Hayden Jul 14 '16 at 20:50
  • 1
    I think that might have to do with breaks. – Lowpar Jul 14 '16 at 20:54
  • 1
    + scale_y_continuous(breaks = seq(0, 100, by = 20)) see - http://stackoverflow.com/questions/22818899/r-ggplot-y-axis-breaks-settings it gives a great presentation on how ggplots works. – Lowpar Jul 14 '16 at 20:55
0

You can use the repeat function in order to get all of the data fleshed out

Data <- rep(V1, each = V2)
hist(Data)

This should do what you want it to and its simple and should be fast

Adam
  • 648
  • 6
  • 18