0

I have the following data:

a <- c(rep(1/9, 80), rep(1/7, 7), rep(1/5, 7), rep(1/3, 6))

How do I choose the ratios 1/7, 1/5 etc as breaks for the x axis? The bars should be as broad as the intervals, i.e. first bar from 1/9-0, second bar from 1/7-1/9 etc.

How do I determine the distribution that has most likely created the data in a convenient way?

Thanks!

user3213255
  • 21
  • 1
  • 1
  • 3
  • What is `a`, `c(...)`, `rep(...)`? What language, IDE, framework, library do you use? Add some context info. – alex Feb 04 '14 at 20:30
  • What I need is the histogram of variable a, which can take the values of the vector c. I use RStudio 0.98.490 on OS X, with the standard framework based on R from CRAN. – user3213255 Feb 04 '14 at 21:15

2 Answers2

1
require("ggplot2")

a <- c(rep(1/9, 80), rep(1/7, 7), rep(1/5, 7), rep(1/3, 6))-0.0001
b <- c(1/10,1/9,1/7,1/5,1/3)

ggplot(NULL, aes(x=a)) + 
                geom_histogram(breaks = b, 
                colour = "black", fill = "lightblue")
Axel
  • 11
  • 1
0
a <- c(rep(1/9, 80), rep(1/7, 7), rep(1/5, 7), rep(1/3, 6))

library(ggplot2)
gg <- data.frame(a)
ggplot(gg)+
  geom_histogram(aes(x=factor(a)),fill="lightgreen")+
  scale_x_discrete(labels=c("1/9","1/7","1/5","1/3"))+
  labs(x="a")

EDIT (Response to OP's comment)

I have a sinking feeling this is what you want:

df<- data.frame(table(a))   # calculate frequencies
df$xmax <- as.numeric(as.character((df$a)))
df$xmin <- c(1/10,df[-nrow(df),]$xmax)
library(ggplot2)
ggplot(df)+
  geom_rect(aes(xmin=xmin, xmax=xmax, ymin=0, ymax=Freq),fill="lightgreen", colour="grey50")+
  scale_x_continuous(breaks=c(1/10,df$xmax),labels=c("1/10","1/9","1/7","1/5","1/3"))

Sorry to have to say it, but this is a truly horrible way to display the data. The eye is naturally drawn to the area, rather than the height, so the frequencies are grossly distorted when you do it this way.

Community
  • 1
  • 1
jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • Thanks! I need the domain from 1/10 to 1 with 4 different intervals: 1/10-1/9, 1/9-1/7, 1/7-1/5 and 1/5-1/3 of proportional lengths (above, they all have the same length.) Each bar should fully cover its interval, i.e. one bar ranging from 1/10-1/9, the next from 1/9-1/7 etc. I appreciate your advice! – user3213255 Feb 05 '14 at 08:40