2017-12-21 9 views
-1

두 개의 다른 y 축과 동일한 x 축을 막대 그래프로 플롯하려고합니다.막대 그래프 2 y 축과 R 언어의 동일한 x 축

어떤 이유로 든 R에서 barplot을 사용하여 플롯 할 수 없습니다. 플롯 기능을 사용하여 동일한 작업을 시도했습니다. 하지만 내가 원하는 것에 다가 갈 수는 없습니다. 코드 시도하는 데 사용됩니다

x,y1,y2 
1,130,1525157 
2,84,1070393 
3,140,1263374 
4,346,2620949 
5,354,2939962 
6,300,3303101 
7,127,1647361 
8,69,1168261 
9,44,7447573 
10,38,12804778 
11,12,570379 
12,22,3100184 
13,7,236046 
14,23,2322048 

은 다음과 같습니다 :

options(scipen=10000000) 
bargraph_test <- read.csv(file="data_test.csv",head=TRUE,sep=",") 
attach(bargraph_test) 
plot(x = x, y = y1, col = "blue", type = "h", xlab = "x", ylab = "y1", main = "") 
par(new = T) 
plot(x = x, y = y1, col = "green", type = "h", xaxt = "n", yaxt = "n", xlab = "", ylab = "") 
axis(4) 
mtext("y2", side = 4, line = 3) 

은 내가 얻을 출력의 여기에 스크린 샷을 첨부하고 여기에

는 데이터입니다.

enter image description here

나는 바 패턴 등이 선을 표시해야합니다.

누군가이 상황을 도와 줄 수 있습니까?

모든 도움에 감사드립니다.

+1

다른 2 Y 축 여기서 상세히 설명된다 https://stackoverflow.com/questions/3099219/plot-with-2-y-axes-one-y-axis-on-the-left - 및 - 다른 - y 축 - - - - 오른쪽 – Linus

답변

1

highcharter을 사용하면 원하는만큼 Y 축을 추가 할 수 있습니다. hc_yAxis_multiples & hc_add_series에 다른 항목을 추가하기 만하면됩니다.

library(highcharter) 

highchart() %>% 
    hc_yAxis_multiples(
    list(lineWidth = 3, lineColor='blue', title=list(text="y1")), 
    list(lineWidth = 3, lineColor="green", title=list(text="y2")) 
) %>% 
    hc_add_series(data = df$y1, color='blue', type = "column") %>% 
    hc_add_series(data = df$y2, color='green', type = "column", yAxis = 1) %>% 
    hc_xAxis(categories = df$x, title = list(text = "x")) 

출력 플롯 : 질문의 마지막 문장으로 Output plot

#sample data 
> dput(df) 
structure(list(x = 1:14, y1 = c(130L, 84L, 140L, 346L, 354L, 
300L, 127L, 69L, 44L, 38L, 12L, 22L, 7L, 23L), y2 = c(1525157L, 
1070393L, 1263374L, 2620949L, 2939962L, 3303101L, 1647361L, 1168261L, 
7447573L, 12804778L, 570379L, 3100184L, 236046L, 2322048L)), .Names = c("x", 
"y1", "y2"), class = "data.frame", row.names = c(NA, -14L)) 
0

실제 질문은, 표시 "어떻게 넓은 바/라인을 어떻게해야합니까?". 저는 그 질문에 답하고 질문 제목에는 질문하지 않습니다.

매개 변수 lwd을 사용하여 선을 더 두껍게 만들 수 있습니다. 막대/선의 끝을 잘 보려면 : answer to 'Increasing the width of type “h” R plot'을보고 lend=1을 사용하십시오.

# data 
x <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14) 
y1 <- c(130,84,140,346,354,300,127,69,44,38,12,22,7,23) 
y2 <- c(1525157,1070393,1263374,2620949,2939962,3303101,1647361,1168261,7447573,12804778,570379,3100184,236046,2322048) 

# what-ever ... 
options(scipen=10000000) 

# first set of bars 
plot(x = x-0.1, y = y1, col = "blue", type = "h", xlab = "x", ylab = "y1", main = "", xlim = range(x), lwd = 4, lend = 1) 
# new: 
# xlim = range(x) :: we need it so that the x-axis is not shifted 
# lwd = 4   :: thicker lines 
# lend = 1  :: flat line ends 
# please note 'x-0.1' 

# add second set of bars 
par(new = T) 
plot(x = x + 0.1, y = y2, col = "green", type = "h", xaxt = "n", yaxt = "n", xlab = "", ylab = "", xlim = range(x), lwd = 4, lend=1) 

# second y-axis 
axis(4) 
mtext("y2", side = 4, line = 3)