Comments (7)
Great!
from ggnewscale.
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.
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"))
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")
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.
(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.
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.
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..
from ggnewscale.
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)
- Issue When Trying to Employ `guide_colourbar` ::::: v0.4.8 HOT 5
- Legend issues when using `guides(...)` HOT 4
- Add ability to name scales HOT 7
- ggnewscale CRAN note HOT 3
- Issues with next version of ggplot2 HOT 4
- Question on overriding scales on a pre-existing ggnewscale plot HOT 3
- Release ggnewscale 0.4.9
- Release ggnewscale 0.4.10
- new_scale_colour does not work with a scale_* function that includes a guide argument HOT 4
- `guide` vs. `show.legend` with `new_scale()` HOT 4
- Does not display two scales HOT 2
- Release ggnewscale 0.5.0
- Release ggnewscale 0.4.10 HOT 4
- new_scale_*() throws an error in ggplot2 3.5.0 HOT 4
- New Scales Clear Custom Attributes HOT 2
- setting legend name in `guides` vs. in `scale_manual` with `new_scale()` HOT 1
- Consider implementing guides with custom hastables HOT 1
- "Reverse" implementation HOT 1
- Release ggnewscale 0.5.0 HOT 1
- v0.5.0 does not add new fill scale in some cases HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ggnewscale.