내가 좋아하는 그래프 만들기 위해 노력하고 이 하나를 사용 ggplot2
하고 gridExtra
패키지,하지만 내 두 개의 플롯이 제대로 다른 미만으로 정렬되지 않습니다. 내 데이터 세트는 coinmarketcap의 비트 코인에 대한 과거 데이터입니다.
library(ggplot2)
library(scales)
library(gridExtra)
bitc$Date <- as.Date(bitc$Date,"%m/%d/%Y")
bitc$Close <- bitc$Close/1000
bitc$Market_Cap <- bitc$Market_Cap/1000000000
bitc$Volume <- bitc$Volume/1000000000
q <- ggplot(bitc,aes(bitc$Date))+
geom_line(aes(y=bitc$Close,colour="Close"))+
theme_bw() +
theme(plot.margin = unit(c(1,5,-30,6),units="points"),
axis.title.y = element_text(vjust =0.25)) +
scale_x_date(breaks=date_breaks("6 months"))+
xlab("Timp")+
ylab("Price (thous. USD)")
q <- q + geom_line(aes(y=bitc$Market_Cap/10,colour="Market_Cap"))
q <- q + scale_y_continuous(sec.axis = sec_axis(~.*10, name = "Market Cap (bld. USD)"))
q
p <- ggplot(bitc, aes(bitc$Date))+
theme_bw() +
theme(plot.margin = unit(c(0,5,1,1),units="points")) +
geom_line(aes(y=bitc$Volume, colour="Volum"))+
scale_x_date(breaks=date_breaks("6 months"))+
scale_color_manual(values="green")+
xlab("Time")+
ylab("Volume (bld. USD)")
p
grid.arrange(q,p,heights = c(4/5,1/5))
의 출력 dput(head(bitc,20))
:
bitc <- structure(list(Date = structure(c(15823, 15824, 15825, 15826,
15827, 15828, 15829, 15830, 15831, 15832, 15833, 15834, 15835,
15836, 15837, 15838, 15839, 15840, 15841, 15842), class = "Date"),
Open = c(135.3, 134.44, 144, 139, 116.38, 106.25, 98.1, 112.9,
115.98, 112.25, 109.6, 113.2, 112.8, 117.7, 115.64, 114.82,
117.98, 111.4, 114.22, 118.21), High = c(135.98, 147.49,
146.93, 139.89, 125.6, 108.13, 115, 118.8, 124.66, 113.44,
115.78, 113.46, 122, 118.68, 117.45, 118.7, 119.8, 115.81,
118.76, 125.3), Low = c(132.1, 134, 134.05, 107.72, 92.28,
79.1, 92.5, 107.14, 106.64, 97.7, 109.6, 109.26, 111.55,
113.01, 113.43, 114.5, 110.25, 103.5, 112.2, 116.57), Close = c(0.13421,
0.14454, 0.139, 0.11699, 0.10521, 0.09775, 0.1125, 0.11591,
0.1123, 0.1115, 0.11357, 0.11267, 0.1172, 0.11524, 0.115,
0.11798, 0.1115, 0.11422, 0.11876, 0.12301), Volume = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
Market_Cap = c(1.488566728, 1.603768865, 1.542813125, 1.298954594,
1.168517495, 1.085995169, 1.250316563, 1.288693176, 1.24902306,
1.2405936, 1.264049202, 1.254535382, 1.30547908, 1.284207489,
1.281982625, 1.315710011, 1.243874488, 1.274623813, 1.325726787,
1.373723882)), .Names = c("Date", "Open", "High", "Low",
"Close", "Volume", "Market_Cap"), row.names = c(NA, 20L), class = "data.frame")
벌써 잘한 것 같아요. cowplot
패키지, 특히 plot_grid
여러 플롯을 그리드에 멋지게 배열 하는 기능 을 사용해 보는 것이 좋습니다 . 여기 에서 패키지에 대한 비 네트를 찾을 수 있습니다 .
library(cowplot)
plot_grid(q,p, align = "hv", ncol = 1, rel_heights = c(4/5, 1/5))
귀하가 제공 한 데이터와 코드를 통해 위의 내용 plot_grid
은 다음과 같습니다.
그건 그렇고, 인터랙티브 플롯을 원한다면 plotly
패키지를 사용해보십시오 . "ggplot"을 사용하여 대화 형으로 만들려고 시도합니다. 다음을 시도해보십시오.
library(plotly)
ggplotly(q)
추가 정보 plotly
: CRAN 페이지 , 기본 웹 페이지 또는 이 책 . ASE의 동료와 교수들에게 깊은 인상을 심어 주면 좋을 것입니다.;) 계속해서 멋진 작업을 & "Sarbatori Fericite!".
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다