Google team has provided R users with very neat visualization package called googleVis. There is a number of options to choose from, it allows the representation of spatial data using US and World maps, as well as regular Google maps. Moreover, your figures are interactive and can easily be inserted into into websites.
Let’s start by installing and loading googleVis package. Remember that you only need to install a package once.
#install.packages("googleVis")
#install.packages("wooldridge")
library(googleVis)
data(wage1, package='wooldridge')
df=data.frame(stat=c("white","nonwhite"),
male= c(mean(wage1$wage[wage1$nonwhite==0&wage1$female==0]), mean(wage1$wage[wage1$nonwhite==1&wage1$female==0])),
female=c(mean(wage1$wage[wage1$nonwhite==0&wage1$female==1]), mean(wage1$wage[wage1$nonwhite==1&wage1$female==1])))
LineChart <- gvisLineChart(df, options=list(title="Average Hourly Wages"))
plot(LineChart)
Another popular visual that is often used by Excel users, is the the bar chart. To use the bar chart, use ‘gvisBarChart’ command.
BarChart <- gvisBarChart(df, options=list(title="Average Hourly Wages"))
plot(BarChart)
To plot the bars vertically, use ‘gvisColumnChart’ command.
ColumnChart <- gvisColumnChart(df, options=list(title="Average Hourly Wages"))
plot(ColumnChart)
Pie chart is typically used to show the distribution or allocation. To see what percentage of all sample is men and women, white and nonwhite adults in our dataset, we can use the following commands.
df2=data.frame(stat=c("female","male"), c(mean(wage1$female),1-mean(wage1$female)))
Pie1 <- gvisPieChart(df2)
plot(Pie1)
df3=data.frame(stat=c("Nonwhite","White"), c(mean(wage1$nonwhite),1-mean(wage1$nonwhite)))
Pie2 <- gvisPieChart(df3)
plot(Pie2)
We can also examine in what kind of industries do the individuals in the sample work.
df4=data.frame(ind=c("Construction","Nondurables Manufacuring","Transp, Comm",
"Trade","Services","Prof. Services"),
share=c( mean(wage1$construc),mean(wage1$ndurman),mean(wage1$trcommpu),
mean(wage1$trade), mean(wage1$services),mean(wage1$profserv)) )
Pie3 <- gvisPieChart(df4)
plot(Pie3)
Even though it does not look very professional, gauge chart can be useful in certain instances. Here, we show the average wages in each industry.
df5=data.frame(ind=c("Constr","ND Mnf","Tra,com",
"Trade","Services","Prof. Serv"),
avgwage=c( mean(wage1$wage[wage1$construc==1]),mean(wage1$wage[wage1$ndurman==1]), mean(wage1$wage[wage1$trcommpu==1]),
mean(wage1$wage[wage1$trade==1]), mean(wage1$wage[wage1$services==1]),mean(wage1$wage[wage1$profserv==1])) )
Gauge <- gvisGauge(df5,
options=list(min=4, max=7, greenFrom=6, greenTo=7,
yellowFrom=5, yellowTo=6,
redFrom=4, redTo=5, width=400, height=300))
plot(Gauge)
Some of the best use of this package has to do with wonderful visual spatial representation of the United States. For example, we can visually show the executions and murder rate by US state using the following commands.
data(murder, package='wooldridge')
murderNoDC=murder[!murder$state=="DC",]
states <- rep(state.name, each=3)
murderNoDC$state1=states;
GeoStates <- gvisGeoChart(murderNoDC[murderNoDC$year==87,], "state1", "exec","mrdrte",
options=list(region="US",
displayMode="regions",
resolution="provinces",
width=600, height=400))
plot(GeoStates)
NewDF <- data.frame(States=state.name,
ExecutionsIn87=murderNoDC[murderNoDC$year==87,5],
ExecutionsIn90=murderNoDC[murderNoDC$year==90,5],
ExecutionsIn93=murderNoDC[murderNoDC$year==93,5],
stringsAsFactors=FALSE)
GeoStates1 <- gvisGeoChart(NewDF, "States", "ExecutionsIn87","ExecutionsIn93",
options=list(region="US",
displayMode="regions",
resolution="provinces",
width=600, height=400))
plot(GeoStates1)
Using data on countries’ populations, we can plot a specific region of interest and data associated in countries of that region. For example, see population data in European countries.
WorldPopulation=data.frame(Country=Population$Country,
Population.in.millions=round(Population$Population/1e6,0),
Rank=paste(Population$Country, "Rank:", Population$Rank))
G5 = gvisGeoChart(WorldPopulation, "Country", "Population.in.millions", "Rank",
options=list(region="150", width=600, height=300))
plot(G5)
Using ‘googleVis’ with ‘WDI’ package that allows us to easily download World Bank development indicators, we can plot GDP per capita (constant 2000 US$) on the interactive World Map. Note that data for a few countries did not load correctly (for example, Russia and Somalia). One would need to make sure the names of countries in ‘data’ are named correctly.
#install.packages('WDI')
library(WDI)
data = WDI(indicator='NY.GDP.PCAP.KD', start=2016, end=2016)
Geo=gvisGeoChart(data, locationvar="country",
colorvar="NY.GDP.PCAP.KD",
options=list(projection="kavrayskiy-vii"))
plot(Geo)