Charlie.Echo
Charlie.Echo

Reputation: 1

Simple Graph in Ggplot2 and Plot.ly for R

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.

Upvotes: 0

Views: 242

Answers (2)

Adam Quek
Adam Quek

Reputation: 7153

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

Upvotes: 1

J.P.
J.P.

Reputation: 55

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.?

Upvotes: 0

Related Questions