1 棒グラフ with barplot

  • G7の一人あたりGDPと人口
library(readxl)
pwt2019 <- read_excel("pwt2019.xlsx")

## 一人あたりGDP
barplot(pwt2019$percapitaGDP,names.arg=pwt2019$countrycode)

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

2 散布図 with plot

  • 人口とGDPの散布図

  • plot(x, y)

  • 参考

plot(pwt2019$rgdpe,pwt2019$pop)

plot(pwt2019$rgdpe,pwt2019$pop, log="xy")

plot(pwt2019$rgdpe,pwt2019$pop, log="xy", xlab="GDP", ylab="Population")

plot(pwt2019$percapitaGDP,pwt2019$pop, log="xy", xlab="Percapita GDP", ylab="Population")

3 折れ線グラフ with plot (1)

  • 一人当たりGDPの折れ線グラフ

  • 参考

library(readxl)
pwt1950 <- read_excel("pwt1950.xlsx")
plot(pwt1950$year,pwt1950$percapitaGDP)

plot(pwt1950$year,pwt1950$percapitaGDP,type = "o")

plot(pwt1950$year,pwt1950$percapitaGDP,type = "l")

JPN<-subset(pwt1950,countrycode=="JPN")
plot(JPN$year,JPN$percapitaGDP,type = "o")

4 折れ線グラフ with plot (2)

4.1 データの切り出し with subset

library(readxl)
pwt1950 <- read_excel("pwt1950.xlsx")
JPN<-subset(pwt1950,countrycode=="JPN")

4.2 日本とアメリカ

USA<-subset(pwt1950,countrycode=="USA")
plot(USA$year,USA$percapitaGDP,type = "o",ylim=c(1000,80000))
points(JPN$year,JPN$percapitaGDP,type = "o")
text(1990,45000,"USA")
text(1990,25000,"JPN")

4.3 日本とドイツ

DEU<-subset(pwt1950,countrycode=="DEU")

plot(DEU$year,DEU$percapitaGDP,type = "o",ylim = c(1000,80000))
points(JPN$year,JPN$percapitaGDP,type = "o")
text(2010,47000,"DEU")
text(2010,35000,"JPN")

4.4 日本とイギリス

GBR<-subset(pwt1950,countrycode=="GBR")

plot(GBR$year,GBR$percapitaGDP,type = "o",ylim = c(1000,80000),col=4)
points(JPN$year,JPN$percapitaGDP,type = "o")
text(2010,47000,"GBR")
text(2010,35000,"JPN")

4.5 日本とフランス

FRA<-subset(pwt1950,countrycode=="FRA")
plot(FRA$year,FRA$percapitaGDP,type = "o",ylim =  c(1000,80000),col=4)
points(JPN$year,JPN$percapitaGDP,type = "o")
text(2010,45000,"FRA")
text(2010,35000,"JPN")

5 GUIで作成1 with guiplot

  • library(guiplot)
  • guiplot()で別ウィンドウが開き、グラフを作成。
  • コンソールのコードを使えば、グラフを再現できる。
library(guiplot)
library(ggplot2)

ggplot() + geom_line(data = pwt1950, aes(x = year, y = percapitaGDP, 
    group = countrycode, color = countrycode)) + geom_point(data = pwt1950, 
    aes(x = year, y = percapitaGDP, group = countrycode, color = countrycode)) + 
    theme_gray()

6 GUIで作成2 with ggplot GUI

  • ggplot GUI
  • https://shiny.gmw.rug.nl/ggplotgui/
  • 上記で、グラフを作成。
  • 出力されるコードを使えば、グラフを再現できる。
  • library(“ggplot2”)が必要。
  • library(“plotly”)も必要。
  • データ名称をdfに変更しておく。

6.1 (1)データ読み込み

データ読み込み
データ読み込み

6.2 (2)グラフ作成

グラフ作成
グラフ作成

6.3 (3)Rコード

Rコード
Rコード
library(readxl)
df <- read_excel("pwt1950.xlsx")
## 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 = year, y = percapitaGDP, colour = countrycode)) +
  geom_point()+
  geom_smooth(se = FALSE, method = 'lm')+
  labs(colour = '') +
  theme_bw() +
  theme(
    legend.position = 'right'
  )
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')

7 参考文献、サイト