Skip to content

What is the easiest way to turn a dataframe into a barplot?

7 messages · yoursurrogategod at gmail.com, Christopher W. Ryan, William Dunlap +3 more

#
Hello, I can't post my code since it's on a work computer.

But basically, I have a dataframe that has two columns, one is a string and the other is an integer.  I want to turn this into a vertival barplot where on the x-axis I have the string in the first columb and then the plot will display the integer count.

I have found many examples online and most of those matched either odd edge cases or putting the data into a format that strips out some of the data and I can't use it later.

This should be a breeze, what am I missing?
#
Here is one way:

dd <- data.frame(var1=c("string1", "string2", "string3"), var2=c(3,7,4))
dd
with(dd, barplot(var2, names.arg=var1))

--Chris Ryan
Binghamton, NY
yoursurrogategod at gmail.com wrote:
#
Does this do what you want?

z <- data.frame(Name=c("One","Three","Twelve","Eleven"), Count=c(1,3,12,11))
with(z, barplot(Count, names=Name, horiz=TRUE))
with(z, barplot(Count, names=Name, horiz=TRUE, las=1))


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, May 12, 2016 at 10:19 AM, yoursurrogategod at gmail.com <
yoursurrogategod at gmail.com> wrote:

            

  
  
#
You are missing a reproducible example. We don't care what is on your home or work computer... we just need to have clear communication, so make up some data that shows the problem and some  code to go with it. You MIGHT have to show some data that does not have the problem in order to highlight the problem for us.
#
Ok, the horizontal names work here.  Thanks.

  
  
#
On Thu, 12 May 2016 13:19:54 -0400
"yoursurrogategod at gmail.com" <yoursurrogategod at gmail.com> wrote:

            
Without showing a data sample, no one can really do more than guess
what you are asking.  Among other things, no one "turns" a
dataframe "into" a barplot, ever.  It would defeat the purpose of
collecting the data to begin with.  It is also unclear what data the
process could strip out, or why you can't use the data again, unless
you are attempting to overwrite the dataframe with the bar plot.  

A barplot is a visual summary of data. So, best to bite the bullet and
supply an example. That way we can understand what you are attempting to
summarize.  As long as it has a similar structure and data types, you
can make it up. You would also benefit a great deal from reading up on
R and communicating in a manner that the community can follow. Sadly, no
specialized community exists without its own specialized jargon and
R-help is an intersection of at least two such communities.

JWDougherty
#
If you want to stay with vertical bars, the barp() function in package plotrix lets you stagger or rotate the labels:
+      "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", 
+      "Fourteen", "Fifteen")
# Note that if you drag the plot window to be wider, the labels will eventually appear
-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of yoursurrogategod at gmail.com
Sent: Thursday, May 12, 2016 1:14 PM
To: William Dunlap
Cc: r-help at r-project.org
Subject: Re: [R] What is the easiest way to turn a dataframe into a barplot?

Ok, the horizontal names work here.  Thanks.
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.