0

I am trying to plot a bar graph with two different y axes and same x axis.

For some reason, I am not able to plot using barplot in R. I tried the same with plot function. But can not come close to what I wanted.

Here is the data:

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 

The code is used to try is below:

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) 

I am attaching the screenshot here of the output I get.

enter image description here

I need to display these lines as bar patterns.

Can someone help me with this situation.

Thanks for all the help.

1

2 Answers 2

2

Using highcharter you can add as many Y-axis you want. Simply add another entry in 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: 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)) 
Sign up to request clarification or add additional context in comments.

Comments

1

As the last sentences of the question indicates, the actual question is "How do I get wider bars/lines?". I answer to that question and not to the question in the questions title.

You can use the parameter lwd to make the lines thicker. To get nice ends of the bars/lines: please see this answer to 'Increasing the width of type “h” R plot' and use 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) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.