When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
library(ggplot2)
data <- 'C:/Users/andre/Downloads/test.txt'
US_trade<-read.table(data,sep="\t",head=TRUE )
data2 <- 'C:/Users/andre/Downloads/test2.txt'
by_country<-read.table(data2,sep="\t",head=TRUE)
by_country_balance<-by_country[2:21,1:10]
by_country_export<-by_country[23:42,1:10]
by_country_import<-by_country[44:63,1:10]
US_trade$order<-1:26
US_trade
## Period Balance.Total Balance.Goods Balance.Services
## 1 2016 January -43409 -64096 20686
## 2 2016 February -45290 -65509 20220
## 3 2016 March -37380 -58003 20623
## 4 2016 April -38422 -58893 20471
## 5 2016 May -41520 -62347 20827
## 6 2016 June -43835 -65206 21372
## 7 2016 July -41294 -62724 21430
## 8 2016 August -41130 -61236 20106
## 9 2016 September -38466 -59435 20969
## 10 2016 October -43069 -63399 20330
## 11 2016 November -46373 -66830 20458
## 12 2016 December -44607 -64829 20222
## 13 2017 January -48692 -69008 20315
## 14 2017 February -44424 -65303 20879
## 15 2017 March -44729 -66072 21343
## 16 2017 April -48057 -68358 20301
## 17 2017 May -47793 -67548 19755
## 18 2017 June -45596 -65366 19770
## 19 2017 July -45385 -65253 19868
## 20 2017 August -44582 -64699 20118
## 21 2017 September -45298 -65344 20046
## 22 2017 October -49098 -69496 20398
## 23 2017 November -50880 -71065 20184
## 24 2017 December -53908 -73700 19792
## 25 2018 January -56665 -76709 20044
## 26 2018 February -57591 -77011 19419
## Export.Total Export.Goods Export.Services Import.Total Import.Goods
## 1 178660 116655 62005 222070 180750
## 2 180892 119138 61754 226182 184648
## 3 179897 117977 61921 217277 175979
## 4 181895 119815 62080 220317 178709
## 5 182166 119760 62407 223686 182106
## 6 183770 120824 62946 227605 186030
## 7 185330 122227 63102 226624 184951
## 8 187385 124075 63310 228514 185311
## 9 188123 124741 63382 226588 184175
## 10 185599 122514 63084 228668 185913
## 11 184848 121653 63196 231221 188483
## 12 189507 126326 63181 234114 191155
## 13 191180 127505 63675 239872 196512
## 14 191793 127634 64159 236217 192937
## 15 191700 126982 64717 236428 193054
## 16 190534 126386 64148 238591 194745
## 17 190841 126548 64292 238633 194096
## 18 193050 128488 64562 238645 193854
## 19 193322 128163 65159 238707 193416
## 20 193721 128352 65370 238303 193051
## 21 195940 129993 65948 241238 195337
## 22 195705 129509 66196 244803 199006
## 23 200208 133943 66265 251089 205008
## 24 203606 137217 66389 257514 210918
## 25 200948 134149 66799 257612 210857
## 26 204445 137179 67266 262037 214190
## Import.Services order
## 1 41319 1
## 2 41534 2
## 3 41298 3
## 4 41609 4
## 5 41580 5
## 6 41575 6
## 7 41673 7
## 8 43203 8
## 9 42413 9
## 10 42754 10
## 11 42738 11
## 12 42959 12
## 13 43359 13
## 14 43280 14
## 15 43374 15
## 16 43847 16
## 17 44538 17
## 18 44792 18
## 19 45291 19
## 20 45252 20
## 21 45902 21
## 22 45797 22
## 23 46080 23
## 24 46596 24
## 25 46755 25
## 26 47847 26
p<-ggplot(US_trade, aes(x=reorder(Period, order), y=Balance.Total,group=1)) + geom_line(aes( colour = "Total")) + geom_point() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
p<-p+geom_line(aes(y = Balance.Services, colour = "Service")) + geom_point(aes(y = Balance.Services, colour = "Service"))
p<-p+geom_line(aes(y = Balance.Goods, colour = "Good")) + geom_point(aes(y = Balance.Goods, colour = "Good"))
p<-p + ylab("Balance")+xlab("Period") + ggtitle("US Trade Balance, Good VS Service") + scale_colour_manual(values=c("red","green","blue"))
p
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
## Warning: package 'tidyr' was built under R version 3.4.4
China_Balance<-by_country_balance %>%
filter(Country.and.Area == "China") %>%
gather("Period",Balance,2:10) %>%
filter(Period != "Year.to.Date.2017") %>%
filter( Period != "Year.to.Date.2018" )
China_Balance$order <- c(7,6,1,2,3,4,5)
China_import<-by_country_import %>%
filter(Country.and.Area == "China") %>%
gather("Period",Import,2:10) %>%
filter(Period != "Year.to.Date.2017") %>%
filter( Period != "Year.to.Date.2018" )
China_import$order <- c(7,6,1,2,3,4,5)
China_export<-by_country_export %>%
filter(Country.and.Area == "China") %>%
gather("Period",Export,2:10) %>%
filter(Period != "Year.to.Date.2017") %>%
filter( Period != "Year.to.Date.2018" )
China_export$order <- c(7,6,1,2,3,4,5)
China_Balance$Import <- China_import$Import
China_Balance$Export <- China_export$Export
p<-ggplot(China_Balance, aes(x=reorder(Period, order), y=Balance,group=1)) + geom_line(aes( colour = "Total")) + geom_point() + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + xlab("Period") + ggtitle("US Trade Import Export from China ")
p<- p+geom_line(aes(y = Import, colour = "Import")) + geom_point(aes(y = Import, colour = "Import"))
p<-p+geom_line(aes(y = Export, colour = "Export")) + geom_point(aes(y = Export, colour = "Export")) + scale_colour_manual(values=c("red","green","blue"))
p
balance<-by_country_balance %>%
gather("Period",Balance,2:10) %>%
filter(Period != "Year.to.Date.2017") %>%
filter( Period != "Year.to.Date.2018" )
order<-select(China_Balance,order,Period)
balance<-left_join(balance,order, "Period")
ggplot(balance, aes(x=reorder(Period, order), y=Balance,group=Country.and.Area)) + geom_line(aes( colour = Country.and.Area)) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + ylab("Balance")+xlab("Period") + ggtitle("US Trade Balance By Country")
t<-balance %>%
group_by(Period) %>%
summarise(Total = sum(Balance))
rat<-right_join(t,balance,"Period")%>%
mutate(ratio = Balance/Total)
ggplot(rat, aes(fill=Period, y=ratio, x=Country.and.Area)) +
geom_bar(position="dodge", stat="identity") + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + ggtitle("US Trade Balance Contirbution By Country")