0

I'm trying to create a very simple graph in R using ggplot2 and Plot.ly. I've got a dataset with about 10 pieces of information. I've read a few tutorials but all of R is lost on me.

This is what I've got, can anyone tell me what I'm doing wrong?

install.packages("ggplot2")
Library("ggplot2")
setwd("c:/Users/charlieecho/documents")
Name data set <- read.(“ATTACKS”, 1)
qplot(LMS, data= ATTACKS, geom=c("scattered"),           main="Number of attacks")

And...

 install.packages("plotly")
 library(plotly)
 setwd("c:/Users/charlieecho/documents")
Name data set <- read.(“ATTACKS”, 1)
plotly graph <- plot_ly(ATTACKS, x = ~number of attacks,   type = "box")

I know some people use the aes style to create a graph but that doesn't make any sense to me either.

I appreciate any help.

  • Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) when you're asking a question. – Adam Quek Apr 17 '17 at 03:06

2 Answers2

1

Here's how you do a simple ggplot and plotly for point plot.

p <- ggplot(dat, aes(x=Year, y=Attacks)) + geom_point()

ggplotly(p)

enter image description here

The data, based on the google doc link:

dat <- structure(list(Year = c(1987, 1988, 1989, 1990, 1991, 1992, 1994, 
1995, 1996), Attacks = c(35, 28, 42, 32, 30, 32, 56, 60, 35)), .Names = c("Year", 
"Attacks"), row.names = c(NA, -9L), class = "data.frame")

For a barplot:

p <- ggplot(dat, aes(x=Year, y=Attacks)) + 
       geom_bar(stat="identity")

ggplotly(p)

enter image description here

Adam Quek
  • 6,973
  • 1
  • 17
  • 23
  • Great, thank you so much. So for Plotly just add "ggplotly(p)" where "ggplot" is in your example? Then add that to what I originally posted, with J.P.'s corrections? For a bar chart I swap out "point" for "bar", correct? – Charlie.Echo Apr 17 '17 at 04:12
  • If you're already starting with ggplot, just use ggplotly to convert it according. Added geom_bar example in the answer. – Adam Quek Apr 17 '17 at 04:16
  • Thanks for all your help. – Charlie.Echo Apr 17 '17 at 04:25
0

I can help a little bit. You can't have spaces in variable names, so replace "Name data set" with:

 mydata <-

Then, what kind of file is your data in? If it's in a .csv file named ATTACKS.csv, you'll want to use:

  mydata <- read.csv("ATTACKS.csv")

Then, in your qplot command, instead of using data=ATTACKS, you'll use:

  data = mydata

If you can share your data, I can try to make the graph, and answer with a complete working script. But when you say, "create a very simple graph", what kind of graph? Time series, histogram, bar chart, etc.?

J.P.
  • 55
  • 1
  • 5
  • Thanks for the tips. I'm looking for a bar chart and maybe a cheat sheet on how to change a few things like color and size, etc. Here's the link to my data, https://docs.google.com/spreadsheets/d/1KVNK0CJ79Yeeg9SVzEQLlEZP_vXQTF5t-oxN0fbHuUw/edit?usp=sharing – Charlie.Echo Apr 17 '17 at 03:27