1 データ読み込み

library(readxl)
asia <- read_excel("asia.xlsx")

2 棒グラフ with barplot

  • アジア各国の人口と生産性
# 生産性
barplot(asia$ctfp,names.arg = asia$countrycode)

# 人口
barplot(asia$pop,names.arg=asia$countrycode)

3 散布図 with plot

  • アジア各国の人口とGDPの関係
# 人口とGDP
plot(asia$pop,asia$rgdpe)

# 人口とGDP(対数)
plot(asia$pop,asia$rgdpe,log="xy")

# 人口とGDP(対数)
plot(asia$pop,asia$rgdpe,log="xy",pch=4)

#  人口とGDP(対数, ラベル付き)
plot(asia$pop,asia$rgdpe,log = "xy",xlab="Population",ylab="GDP")

4 データ読み込み

  • シンガポール、アルゼンチン、香港の比較
library(readxl)
pwt1950sgp <- read_excel("pwt1950sgp.xlsx")
df <- pwt1950sgp

5 静的グラフ with ggplot2

library("ggplot2")

# グラフ
graph <- ggplot(df, aes(x = year, y = percapitaGDP, colour = countrycode)) +
  geom_point()+
  geom_smooth(se = FALSE, method = 'lm') +
  theme_bw()
graph

6 動的グラフ with plotly

library("plotly")
ggplotly(graph)
# If you would like to save your graph, you can use:
ggsave('my_graph.pdf', graph, width = 14, height = 14, units = 'cm')

7 データ読み込み

  • 以下でGUIでグラフ作成するため、データ名を「df」にしておく。
library(readxl)
df <- read_excel("asia.xlsx")

8 GUIでグラフ作成 with ggplotgui

  • 生産性(TFP)と一人当たりGDPの関係を表す。
  • library(ggplotgui)をインストール
  • コンソールにggplot_shiny()と打つ
  • グラフ作成して、Rコードを得る。
## You can use the below code to generate the graph.
## Don't forget to replace the 'df' with the name
## of your dataframe

# You need the following package(s):
library("ggplot2")

# The code below will generate the graph:
graph <- ggplot(df, aes(x = ctfp, y = percapitaGDP)) +
  geom_point()+
  geom_smooth(se = TRUE, method = 'lm') +
  theme_bw()
graph

# If you want the plot to be interactive,
# you need the following package(s):
library("plotly")
ggplotly(graph)
# If you would like to save your graph, you can use:
ggsave('my_graph.pdf', graph, width = 14, height = 14, units = 'cm')