ggplot2 bar chart sign on y-axis

user8491385

I have the following data :

mydata <- data.frame(x = c("UK1", "UK2", "UK3", "UK4", "UK5", "UK6", "UK7"), 
                     n = c(50, 55, 58, 64, 14, 18, 45), 
                     F = c(-6, 17, 26, -37, 44, -22, 15), 
                     z = c("a", "a", "b", "a" , "b", "b", "a"))

I want to create a ggplot (bar chart) of column x (x axis) against column n (y-axis) colour split by column z. The tricky part is I want to bar chart to be going up the way if the value in F is positive and down the way if negative. Is this possible with ggplot?

PoGibas

Solution using sign:

You can use sign() to extract sign of F and multiply that by n: n * sign(F)

library(ggplot2)

mydata <- data.frame(x = c("UK1", "UK2", "UK3", "UK4", "UK5", "UK6", "UK7"), 
                     n = c(50, 55, 58, 64, 14, 18, 45), 
                     F = c(-6, 17, 26, -37, 44, -22, 15), 
                     z = c("a", "a", "b", "a" , "b", "b", "a"))

ggplot(mydata, aes(x, n * sign(F), fill = z)) +
    geom_bar(stat = "identity", position = "dodge")

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to arrange y axis in bar chart using ggplot2

ggplot2 bar chart gives weird y axis

Grouped stacked bar chart in ggplot2 where each stack corresponds to its y axis value

Manually change order of y axis items on complicated stacked bar chart in ggplot2

How to modify the limits of y axis of a stacked bar chart in polar coordinates using ggplot2?

Can get ggplot2 bar chart to display direct values for Y axis?

Combining Bar and Line chart (double axis) in ggplot2

ggplot2: negative bar chart with dual x axis

R: ggplot stacked bar chart with counts on y axis but percentage as label

Breaking y-axis in ggplot2 with geom_bar

How to add Thousand separator in both axis of a 2-y axis chart in R using ggplot2

Zingchart bar chart starting at y axis -50

Y axis in single stacked bar chart

weird y-axis dodged bar chart

Horizontal bar chart y axis alignment

Indicating a range for the Y axis in a bar chart

In ggplot geom_bar, the y axis labels are cluttered at the bottom of bar chart

ggplot2 Bar Graph remove unnecessary distance between axis label and 0 count on y axis

Offline Plotly: Generate bar chart of 2 columns on (y axis) & 1 column on ( x axis)

how to remove minus sign from "negative area" of y axis (ggplot2 r)

Stacked bar chart swap x-axis and y-axis

How to apply dollar sign in Y- axis in chart js?

How do to conditionally bold y axis text in gantt chart using ggplot2

Reorder x axis in bar chart in ggplot

Make Y-axis start at 1 instead of 0 within ggplot bar chart

Wrapping text and increasing the space between factors on the y-axis in a ggplot bar chart in R?

Show percent of total on top of geom_bar in ggplot2 while showing counts on y axis

R & ggplot2: 100% geom_bar + geom_line for average using secondary y axis

Adjusting y axis origin for stacked geom_bar in ggplot2