繪製 line plot
par(mfrow= c(1,1))
x <- seq(1,6)
y <- seq(1,6)
title <- 'hello wolrd'
plot(x,y, type='n', main = title)
lines(x, y, type = 'l', col = "red")

par(mfrow = c(2,4))
types =c("p","l","o","b","c","s","h","n")
for(i in 1:length(types)){
title=paste("type: ",types[i])
plot(x, y, type="n", main=title)
lines(x, y, type=types[i])
}

par(mfrow=c(1,1))
taipei <- c(92.5,132.6,168.8,159.1,218.7)
tainan <-c(21.2, 30.6, 37.3, 84.6, 184.3)
plot(taipei, type="o", col="blue", ylim=c(0,220), xlab="Month",ylab="Rainfall")
lines(tainan, type="o", pch=22, lty=2, col="red")

繪製 bar chart
download.file('https://github.com/ywchiu/rtibame/raw/master/data/house-prices.csv', destfile = 'house-prices.csv')
housePrice=read.csv('house-prices.csv',header=TRUE)
bedrooms <- housePrice$Bedrooms
bedroomsTable <- table(bedrooms)
barplot(bedroomsTable, main="Bedroom Type Calculate",xlab="bedroom type", ylab="count")

繪製 Histogram
weight = cdc$weight
hist(weight,breaks=1000)
table(cdc$weight %% 10)
par(mfrow=c(2,1))
hist(weight,breaks=50,xlim=c(70,380))
barplot(table(cdc
$weight),xlab="weight",ylab="Frequency")
繪製Pie 圖
par(mfrow=c(1,1))
housePrice = read.csv('house-prices.csv',header = TRUE)
bedrooms = housePrice$Bedrooms
bedroomsTable = table(bedrooms)
names(bedroomsTable) = c("2 unit", "3 unit", "4 unit", "5 unit")
pie(sort(bedroomsTable, decreasing = TRUE),
col=rainbow(length(bedroomsTable)),
main="Pie Chart of Bedroom")

繪製散佈圖
plot(cdc$weight, cdc$wtdesire)
data(iris)
xlab <- 'Sepal.Length'
ylab <- 'Petal.Length'
x = iris[,xlab]
y = iris[,ylab]
plot(x, y, xlab=xlab, ylab=ylab,type="n")
setosa <- iris[iris$Species == 'setosa', ]
points(setosa[,xlab], setosa[,ylab], col="green")
versicolor <- iris[iris$Species == 'versicolor', ]
points(versicolor[,xlab], versicolor[,ylab], col="red")

加上趨勢線
plot(cdc$weight, cdc$wtdesire,
xlab="weigtht",ylab="weight desire",
main="Scatter of Weight")
fit <- lm(wtdesire ~ weight, data=cdc)
abline(fit, col = "red")
繪製所有變量關係
data(iris)
plot(iris)

plot(iris, col = iris$Species)

par(mfrow=c(2,2))
plot(iris$Species ~ ., data=iris)

繪製mosaic plot
par(mfrow=c(1,1))
smokers_gender = table(cdc$gender, cdc$smoke100)
colnames(smokers_gender) = c("no","yes")
mosaicplot(smokers_gender
,col=rainbow(length(colnames(smokers_gender))))
繪製box plot
boxplot(cdc$height,
ylab="Height",
main="Box Plot of Height")
hist(cdc$height)
boxplot(cdc$height ~ cdc$gender
,ylab="Height",xlab="Gender"
,main="Height vs Gender")
temp <- sample.int(30, 100, replace=TRUE)
mean(temp)
median(temp)
boxplot(temp)
temp <- c(temp, 999,999 )
mean(temp)
median(temp)
boxplot(temp)
圖的輸出
for(i in 1:4){
name <-paste("iris",i, ".jpg", sep="")
jpeg(name, width=800, height=800)
plot(iris[,i])
dev.off()
}
使用plotly 繪製 pie 圖
library(plotly)
## Warning: package 'plotly' was built under R version 3.2.5
## Loading required package: ggplot2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
ds <- data.frame(labels = c('A', 'B', 'C'),
values = c(10 , 20 , 30))
plot_ly(ds, type ="pie",
labels = ds$labels,
values = ds$values,
hole=0.6)%>%
layout(title="Donut Chart Example")
使用plotly繪製area chart
month<-c(1,2,3,4,5)
taipei<-c(92.5,132.6,168.8,159.1,218.7)
tainan <-c(21.2, 30.6, 37.3, 84.6, 184.3)
plot_ly(x = month, y = taipei, type='scatter', mode='lines',name="taipei") %>% add_trace(x = month, y = tainan ,name="tainan")
y <-list(title="Rainfall")
plot_ly(x = month, y = taipei, fill = "tozeroy", name="taipei", type='scatter', mode= 'markers') %>% add_trace(x = month, y = tainan, fill = "tozeroy" ,name="tainan") %>% layout(yaxis= y)
total <- taipei + tainan
plot_ly(x = month, y = taipei, fill = "tozeroy", name="taipei", type='scatter', mode= 'markers') %>% add_trace(x = month, y = total, fill = "tozeroy" ,name="tainan") %>% layout(yaxis= y)
使用plotly 繪製bubble chart
data("diamonds")
# scatter chart
plot_ly(data= diamonds,
x = diamonds$carat,
y = diamonds$price,
type='scatter',
mode='markers')
d <-diamonds[sample(nrow(diamonds),1000), ]
plot_ly(data= diamonds,
x = diamonds$carat,
y = diamonds$price,
color = diamonds$clarity,
size = diamonds$carat,
text=paste("Clarity: ", diamonds$clarity),
type='scatter',
mode='markers')
使用plotly 繪製HeatMap
m <-matrix(rnorm(9), nrow=3, ncol=3)
plot_ly(z =m,x =c("a", "b", "c"), y =c("d", "e", "f"),type ="heatmap")
df<-read.csv("https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv")
df$hover <- with(df, paste(state, '<br>', "Beef", beef, "Dairy", dairy, "<br>",
"Fruits", total.fruits, "Veggies", total.veggies,
"<br>", "Wheat", wheat, "Corn", corn))
# give state boundaries a white border
l <- list(color = toRGB("white"), width = 2)
# specify some map projection/options
g <- list(
scope = 'usa',
projection = list(type = 'albers usa'),
showlakes = TRUE,
lakecolor = toRGB('white')
)
plot_geo(df, locationmode = 'USA-states') %>%
add_trace(
z = ~total.exports,
text = ~hover,
locations = ~code,
color = ~total.exports, colors = 'Purples'
) %>%
colorbar(title = "Millions USD") %>%
layout(
title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)',
geo = g
)
Multiple Plot
data("economics")
p <-subplot(
plot_ly(economics, x =economics$date, y =economics$uempmed, type = 'scatter', mode='line'),
plot_ly(economics, x =economics$date, y =economics$unemploy, type = 'scatter', mode='line'),
margin =0.05,nrows=1
)%>%layout(showlegend=FALSE)
## A line object has been specified, but lines is not in the mode
## Adding lines to the mode...
## A line object has been specified, but lines is not in the mode
## Adding lines to the mode...
p
使用GoogleVis
library(googleVis)
## Warning: package 'googleVis' was built under R version 3.2.5
## Creating a generic function for 'toJSON' from package 'jsonlite' in package 'googleVis'
##
## Welcome to googleVis version 0.6.1
##
## Please read the Google API Terms of Use
## before you start using the package:
## https://developers.google.com/terms/
##
## Note, the plot method of googleVis will by default use
## the standard browser to display its output.
##
## See the googleVis package vignettes for more details,
## or visit http://github.com/mages/googleVis.
##
## To suppress this message use:
## suppressPackageStartupMessages(library(googleVis))
month<-c(1,2,3,4,5)
taipei<-c(92.5,132.6,168.8,159.1,218.7)
tainan <-c(21.2, 30.6, 37.3, 84.6, 184.3)
df<-data.frame(month=month,taipei=taipei,tainan=tainan)
Line<-gvisLineChart(df)
plot(Line)
## starting httpd help server ...
## done
Line<-gvisLineChart(df,"month", c("taipei","tainan"),options=list(series="[{targetAxisIndex: 0},{targetAxisIndex:1}]",vAxes="[{title:'taipei'}, {title:'tainan'}]"))
data("CityPopularity")
Pie <-gvisPieChart(CityPopularity,options=list(width =600,height =400))
plot(Pie)
data("Exports")
Geo=gvisGeoChart(Exports, locationvar="Country",colorvar="Profit",options=list(projection="kavrayskiy-vii"))
plot(Geo)
data(Andrew)
AndrewMap<-gvisMap(Andrew,
locationvar="LatLong",
tipvar="Tip",)
plot(AndrewMap)
data(Fruits)
M =gvisMotionChart(Fruits,idvar="Fruit", timevar="Year",options=list(width =600, height =400))
plot(M)
#stock <- read.csv('stock.csv', header=TRUE)
#M =gvisMotionChart(stock,idvar="stockid", timevar="年度",options=list(width =600, height =400))
#plot(M)