GithubHelp home page GithubHelp logo

Comments (7)

efratushava avatar efratushava commented on September 2, 2024 1

Great!

from ggnewscale.

eliocamp avatar eliocamp commented on September 2, 2024

Hi!
I don't understand what is the expected result, so I'm not sure what's wrong with the plot. In any case, looking at your code, you might not need ggnewscale at all! So here are some general recommendations that might clarify the issue.

If your issue is that you are trying to draw black and yellow bars, then you need to put the fill parameter outside the aes() specification. (See this section of R for data science). Doing that, you don't need ggnewscale because you are not using a new fill scale.

It's also possible is to use your code and add scale_fill_identity() (for that you do need ggnewscale)

Let me know if it's useful for you and if not, can you post a reproducible example that includes the data? You can use the awesome reprex package for that.

from ggnewscale.

efratushava avatar efratushava commented on September 2, 2024

I'll try to explain:
I want to make a plot with a geom bar on geom bar, where the front bar is deviding the background bar into 2 groups, where the front and background color is determined by what I want,

for example,
Here is the data:

data <- data.frame(name=c("a","b","c"),background=c(5,10,20),front_half1=c(2,4,17))
data$front_half2 <- data$background-data$front_half1
data <- reshape2::melt(data, measure.vars = c("front_half1","front_half2"),value.name = "value", variable.name = "half")

ggplot(data, aes(x=name,y=background, fill=name))+
  geom_bar(position=position_dodge(), stat="identity", color="grey15") +
scale_fill_manual(values = c("green","black","blue"))

image

As you can see, before adding "scale_fill_new" the data is in color green,black,blue as I determined.
Adding scale_fill_new remove the determind color from the background"

data <- data.frame(name=c("a","b","c"),background=c(5,10,20),front_half1=c(2,4,17))
data$front_half2 <- data$background-data$front_half1
data <- reshape2::melt(data, measure.vars = c("front_half1","front_half2"),value.name = "value", variable.name = "half")

ggplot(data, aes(x=name,y=background, fill=name))+
  geom_bar(position=position_dodge(), stat="identity", color="grey15") +
scale_fill_manual(values = c("green","black","blue"))+
   new_scale_fill()+ 
  geom_bar(aes(x=name,y=value,fill=half),stat="identity",position=position_dodge(), color="grey45")

image

Also, I must used "aes" if I want the data in the front bar plot to be divided into 2 groups

Hope it is much understandable!

from ggnewscale.

eliocamp avatar eliocamp commented on September 2, 2024

(I edited a bit of the formatting of your post)

Ah, thanks! I think I know what the issue is now. For now, and as a workaround, you can get what you want if you define the fill aesthetic inside the geom_*() call and not in the global ggplot() call:

library(ggplot2)
library(ggnewscale)

data <- data.frame(name=c("a","b","c"),background=c(5,10,20),front_half1=c(2,4,17))
data$front_half2 <- data$background-data$front_half1
data <- reshape2::melt(data, measure.vars = c("front_half1","front_half2"),value.name = "value", variable.name = "half")

ggplot(data, aes(x=name,y=background))+
  geom_bar(position=position_dodge(), stat="identity", color="grey15", aes(fill = name)) +
  scale_fill_manual(values = c("green","black","blue"))

ggplot(data, aes(x=name,y=background))+
  geom_bar(aes(fill = name), position=position_dodge(), stat="identity", color="grey15") +
  scale_fill_manual(values = c("green","black","blue"))+
  new_scale_fill()+ 
  geom_bar(aes(x=name,y=value,fill=half),stat="identity",position=position_dodge(), color="grey45")

Created on 2019-08-27 by the reprex package (v0.3.0)

from ggnewscale.

eliocamp avatar eliocamp commented on September 2, 2024

Thanks for finding the bug!
It should be fixed now in the latest commit. You can use the workaround I showed above or use your original code.

library(ggplot2)
library(ggnewscale)

data <- data.frame(name=c("a","b","c"),background=c(5,10,20),front_half1=c(2,4,17))
data$front_half2 <- data$background-data$front_half1
data <- reshape2::melt(data, measure.vars = c("front_half1","front_half2"),value.name = "value", variable.name = "half")

ggplot(data, aes(x=name,y=background, fill=name))+
  geom_bar(position=position_dodge(), stat="identity", color="grey15") +
  scale_fill_manual(values = c("green","black","blue"))+
  new_scale_fill()+ 
  geom_bar(aes(x=name,y=value,fill=half),stat="identity",position=position_dodge(), color="grey45")

Created on 2019-08-27 by the reprex package (v0.3.0)

from ggnewscale.

efratushava avatar efratushava commented on September 2, 2024

Thank you for the detailed answer!

And what if I want guide only to the second bar?

Running this:
ggplot(data, aes(x=name,y=background))+ geom_bar(aes(fill = name), position=position_dodge(), stat="identity", color="grey15") + scale_fill_manual(values = c("green","black","blue"))+ guides(fill=F)+ new_scale_fill()+ geom_bar(aes(x=name,y=value,fill=half),stat="identity",position=position_dodge(), color="grey45")

leaves me with the guide for geom_bar of the background data, and I want only for the front data, a.k.a the second bar plot..
image

from ggnewscale.

eliocamp avatar eliocamp commented on September 2, 2024

Something like this?

library(ggplot2)
library(ggnewscale)

data <- data.frame(name=c("a","b","c"),background=c(5,10,20),front_half1=c(2,4,17))
data$front_half2 <- data$background-data$front_half1
data <- reshape2::melt(data, measure.vars = c("front_half1","front_half2"),value.name = "value", variable.name = "half")


ggplot(data, aes(x=name,y=background)) +
   geom_bar(aes(fill = name), position=position_dodge(), stat="identity", color="grey15") + 
   scale_fill_manual(values = c("green","black","blue"), guide = "none") +   # <- Remove the guide here
   new_scale_fill() + 
   geom_bar(aes(x=name,y=value,fill=half),stat="identity",position=position_dodge(), color="grey45")

Created on 2019-08-27 by the reprex package (v0.3.0)

from ggnewscale.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.