How can I create a histogram in R?


sara

I'm new to working in Unix, so I need help on how to put a histogram together using R in Linux environment?

File:

48302   50  0
48303   46  0
48304   45  0
48305   41  15
48306   44  21
48307   74  0
48308   71  0
48309   35  19
48310   66  0
48311   26  42
48312   44  23
48313   69  0
48314   77  0
48315   64  0
48316   60  3
48317   60  2
48318   62  15
48319   71  9
48320   65  13
48321   88  0
48322   4   29

I need to create a histogram using the data from the 3rd column.

Hi

If you put the data in your example into a file, sample.txt, you can then invoke R and do the following:

$ R

Now you're at a R prompt:

> d = read.table('sample.txt',col.name=c("col1","col2","col3"))

You can confirm that the data was loaded correctly into table d using the dim command:

> dim(d)
[1] 21  3

Now you can graph column 3 (col3) as we labeled it above when we read it in from the file, like so:

> hist(d$col3)

Resulting in this plot:

     ss of histogram

Running it as a single script

If you want you can create the following .r file, call it hist.r:

d = read.table('sample.txt',col.name=c("col1","col2","col3"))
dim(d)
hist(d$col3)

Then run it using R's Rscript command like this:

$ Rscript hist.r
[1] 21  3

This will appear to have done nothing, but it automatically will put a .pdf file in the directory from where you ran it with the contents of the histogram in it.

$ ls -l
total 24
-rw-rw-r-- 1 saml saml    80 Sep 11 02:35 hist.r
-rw-rw-r-- 1 saml saml 12840 Sep 11 02:37 Rplots.pdf
-rw-rw-r-- 1 saml saml   302 Sep 11 02:19 sample.txt

You can customize this so that instead of a .pdf file you'll get a .png file or what have you.

References

Related


How can I make this histogram in ggplot/R?

Crimea Please find My data qbelow . I have two covariates: q$Studieand q$best.respcorresponding to each of the five different studies reporting the best response after a certain treatment. q$best.respThere are three levels table(q$best.resp) 0 1 2 62 42

How can I make this histogram in ggplot/R?

Crimea Please find My data qbelow . I have two covariates: q$Studieand q$best.respcorresponding to each of the five different studies reporting the best response after a certain treatment. q$best.respThere are three levels table(q$best.resp) 0 1 2 62 42

How to create a histogram? [R

Sylina I am trying to create a histogram using frequency data. Data are as follows: x freq 1 Buick 80 2 Cadillac 80 3 Chevrolet 320 4 Pontiac 150 5 SAAB 114 6 Saturn 60 When I try: hist(carMake) I get: Then I tried: df = as.da

How to create a histogram? [R

Sylina I am trying to create a histogram using frequency data. Data are as follows: x freq 1 Buick 80 2 Cadillac 80 3 Chevrolet 320 4 Pontiac 150 5 SAAB 114 6 Saturn 60 When I try: hist(carMake) I get: Then I tried: df = as.da

How to create a histogram in R?

Sarah I'm new to working on Unix, so I need help how to put together a histogram using R in a Linux environment? document: 48302 50 0 48303 46 0 48304 45 0 48305 41 15 48306 44 21 48307 74 0 48308 71 0 48309 35 19 48310 66 0 48311

How to create a histogram? [R

Sylina I am trying to create a histogram using frequency data. Data are as follows: x freq 1 Buick 80 2 Cadillac 80 3 Chevrolet 320 4 Pontiac 150 5 SAAB 114 6 Saturn 60 When I try: hist(carMake) I get: Then I tried: df = as.da

How to create a histogram? [R

Sylina I am trying to create a histogram using frequency data. Data are as follows: x freq 1 Buick 80 2 Cadillac 80 3 Chevrolet 320 4 Pontiac 150 5 SAAB 114 6 Saturn 60 When I try: hist(carMake) I get: Then I tried: df = as.da

How to create a histogram? [R

Sylina I am trying to create a histogram using frequency data. Data are as follows: x freq 1 Buick 80 2 Cadillac 80 3 Chevrolet 320 4 Pontiac 150 5 SAAB 114 6 Saturn 60 When I try: hist(carMake) I get: Then I tried: df = as.da

how do i create this histogram

so I am trying to create a histogram for salary variable my code is library(tidyverse) library(broom) library(dplyr) df=read.csv("/Users/takonyabadza/Desktop/Assign6data.csv") select(df, wage) W<-data.frame(df) ggplot(W, aes(x = wage)) + geom_histogram() logW

How can I colour specific data bars in R Histogram

David Dickson If I have a histogram: > hist(faithful$waiting,seq(min(faithful$waiting),max(faithful$waiting))) and a list of "special" frequencies: > c(51, 52, 57, 59, 64) is it possible to colour the bars corresponding to these special frequencies a differe

How can I colour specific data bars in R Histogram

David Dickson If I have a histogram: > hist(faithful$waiting,seq(min(faithful$waiting),max(faithful$waiting))) and a list of "special" frequencies: > c(51, 52, 57, 59, 64) is it possible to colour the bars corresponding to these special frequencies a differe

How can I fix the histogram

aa I am trying to draw a very simple histogram using python. Here is my code: from numpy import * from matplotlib.pyplot import* from random import* nums = [] N = 10 for i in range(N): a = randint(0,10) nums.append(a) bars= [0,1,2,3,4,5,6,7,8,9] hist(

How can I fix the histogram

aa I am trying to draw a very simple histogram using python. Here is my code: from numpy import * from matplotlib.pyplot import* from random import* nums = [] N = 10 for i in range(N): a = randint(0,10) nums.append(a) bars= [0,1,2,3,4,5,6,7,8,9] hist(

How to create a histogram from a dataframe in R

David Kizonis I want to create a histogram from a data frame, but I get an error every time I use the code 'x' must be numeric. df <- data.frame(col1 = c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120), col2 = c(10, 21, 31, 41, 51, 61, 72, 82, 92, 1

R question: how to create a histogram with two variables?

rough Well, let me address my question as clearly as possible. I'm new to R, so bear with me. I want to create a histogram using two different vectors. The first vector contains a list of models (products). The models are listed as integers, strings or NA. I'm

How to create a histogram from a dataframe in R

David Kizonis I want to create a histogram from a data frame, but I get an error every time I use the code 'x' must be numeric. df <- data.frame(col1 = c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120), col2 = c(10, 21, 31, 41, 51, 61, 72, 82, 92, 1

How to create a histogram from a dataframe in R

David Kizonis I want to create a histogram from a data frame, but I get an error every time I use the code 'x' must be numeric. df <- data.frame(col1 = c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120), col2 = c(10, 21, 31, 41, 51, 61, 72, 82, 92, 1

How to create a histogram from a dataframe in R

David Kizonis I want to create a histogram from a data frame, but I get an error every time I use the code 'x' must be numeric. df <- data.frame(col1 = c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120), col2 = c(10, 21, 31, 41, 51, 61, 72, 82, 92, 1

How to create a histogram from a dataframe in R

David Kizonis I want to create a histogram from a data frame, but I get an error every time I use the code 'x' must be numeric. df <- data.frame(col1 = c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120), col2 = c(10, 21, 31, 41, 51, 61, 72, 82, 92, 1

R question: how to create a histogram with two variables?

rough Well, let me address my question as clearly as possible. I'm new to R, so bear with me. I want to create a histogram using two different vectors. The first vector contains a list of models (products). The models are listed as integers, strings or NA. I'm