Histogram conditional fill color
It would be easiest to just add another column with the condition and update the aes
to include the fill group.
cust$high_rev <- as.factor((cust[,2]>100000)*1)
ggplot(cust, aes(cust_rev, fill=high_rev)) +
geom_histogram(color="black", binwidth=1/3) +
scale_x_log10(labels=comma, breaks=powers(10,8)) +
scale_y_continuous(labels=comma) +
xlab("Customer Revenue") + ylab("Number of Customers") +
ggtitle("Distribution of Customer Value")
If you have your heart set on some specific colors you can use the scale_fill_manual
function. Here is an example with some fun bright colors.
ggplot(cust, aes(cust_rev, fill=high_rev)) +
geom_histogram(color="black", binwidth=1/3) +
scale_x_log10(labels=comma, breaks=powers(10,8)) +
scale_y_continuous(labels=comma) +
scale_fill_manual(values = c("green", "purple")) +
xlab("Customer Revenue") + ylab("Number of Customers") +
ggtitle("Distribution of Customer Value")
Create a histogram in a cell based on condition
I've created a similar graph on one of my dashboards. My output:
You have to work backwards a bit, the red/yellow/green color is just a background color, and is set by conditional formatting based on value, and doesn't change in size. To give it the appearance that it is growing or shrinking, you need a grey/white bar conditional format that "covers" up the color on a right to left basis.
Conditional Rules:
Blank bar conditional formatting:
In each of the "bar" cells, you'll just need to point to the % you want to use for each bar and subtract one. So = 1 - percentage
will give you the value that the blank bar conditional formatting will pick up.
I also used custom format ;;;
to hide the text in the cells:
Let me know if this works for your purposes!
Filling bar colours with the mean of another continuous variable in ggplot2 histograms
If you want a genuine histogram you need to transform your data to do this by summarizing it first, and plot with geom_col
rather than geom_histogram
. The base R function hist
will help you here to generate the breaks and midpoints:
library(ggplot2)
library(dplyr)
mtcars %>%
mutate(mpg = cut(x = mpg,
breaks = hist(mpg, breaks = 0:4 * 10, plot = FALSE)$breaks,
labels = hist(mpg, breaks = 0:4 * 10, plot = FALSE)$mids)) %>%
group_by(mpg) %>%
summarize(n = n(), wt = mean(wt)) %>%
ggplot(aes(x = as.numeric(as.character(mpg)), y = n, fill = wt)) +
scale_x_continuous(limits = c(0, 40), name = "mpg") +
geom_col(width = 10) +
theme_bw()
How to color a ggplot histogram differently based on precise cut off points?
Maybe this'll work for you. You can specify the bin-breaks in geom_histogram. So we first create an evenly spaced bin-vector and add some cutoff points to it:
n.bins <- 5 # number of bins
additional.cutoffs <- c(3.9, 2.9) # additional bins
bins <- seq(min(mtcars$wt), max(mtcars$wt), length.out = n.bins)
bins <- c(bins, additional.cutoffs) %>% sort()
mtcars %>%
mutate(wt_color = ifelse(wt < cutoff_point, "red", "blue")) %>%
select(wt, wt_color) %>%
ggplot(aes(x=wt, fill = wt_color)) +
geom_histogram(breaks = bins) +
geom_vline(xintercept=additional.cutoffs, colour="black")
Conditionally changing the colour of a histogram in ggplot, and not displaying both bar outlines?
I worked it out, the solution was adding drop=TRUE
to geom_histogram
to drop bins with zero counts. This then removes prevents the overlaying of the outline.
ggplot(data=dat, aes(x=val,colour=name, fill=name)) +
geom_histogram(alpha=0.4, data=subset(dat, name %in% c("A","C"))) +
geom_histogram(alpha=0.4, data=subset(dat, val<100 & name=="B"), fill="grey", colour="grey") +
geom_histogram(alpha=0.4, data=subset(dat, val>=100 & name=="B"), drop=T) +
facet_wrap(~name, ncol=1)
Custom fill colour of bin with highest density in geom_histogram
You need to set the fill
argument to a factor
which takes 2 levels: one for all the density values lower then the max
and one for the maximum density:
ggplot(a, aes(x = Middlepoint)) +
geom_histogram(aes(y = ..density..,
fill = cut(..density.., c(0, sort(..density.., TRUE)[1:2]))),
binwidth = 1) +
scale_fill_manual("", values = c("red", "green")) +
theme_minimal()
Plot with conditional colors based on values in hist
You could determine the colors according to the mid-point of each bin.
Sample <- rnorm(2500, 1000, 20)
h <- hist(Sample, plot = FALSE)
hist(Sample, col = ifelse(h$mids < 980, 'red', 'green'))
gnuplot conditional fill color
If I understand correctly, you can try one of the approaches from this question, for example this answer. There you also find the explanations.
set terminal pngcairo size 960,600
set output "conditional_fill.png"
datafile="themesdat.txt"
set yrange [0:100]
set key out
set style data histograms
set style histogram rowstacked
set boxwidth 0.8 relative
set style fill solid 1.0 border -1
set style line 3 lt 1 lc rgb "black"
set style line 4 lt 1 lc rgb "green"
set style line 5 lt 1 lc rgb "yellow"
set style line 6 lt 1 lc rgb "magenta"
set style line 7 lt 1 lc rgb "red"
set style line 13 lt 1 lc rgb "red"
set style line 14 lt 1 lc rgb "magenta"
set style line 15 lt 1 lc rgb "yellow"
set style line 16 lt 1 lc rgb "green"
set style line 17 lt 1 lc rgb "black"
color(x,y) = 10*x + y
stats datafile u 2 nooutput
n = STATS_records
set multiplot
do for [i=0:n-1] {
plot datafile u (0):xticlabels(1) notitle,\
newhistogram "" at i, \
"" every ::i::i u (style = color($2, 3), 0) notitle, \
"" every ::i::i u 3 ls style title "severe problems", \
"" every ::i::i u (style = color($2, 4), 0) notitle, \
"" every ::i::i u 4 ls style title "probs", \
"" every ::i::i u (style = color($2, 5), 0) notitle, \
"" every ::i::i u 5 ls style title " ", \
"" every ::i::i u (style = color($2, 6), 0) notitle, \
"" every ::i::i u 6 ls style title "help?", \
"" every ::i::i u (style = color($2, 7), 0) notitle, \
"" every ::i::i u 7 ls style title "fine"
}
unset multiplot
This is the result:
Single histogram with two or more colors depending on xaxis values
The hist function uses the pretty function to determine break points, so you can do this:
set.seed(69)
x <- rnorm(500)
breaks <- pretty(x,10)
col <- ifelse(1:length(breaks) <= length(breaks)/2, "red", "blue")
hist(x, col = col, breaks = breaks)
Related Topics
Using Lm in List Column to Predict New Values Using Purrr
How to Use Tidyr to Fill in Completed Rows Within Each Value of a Grouping Variable
Print Tibble with Column Breaks as in V1.3.0
Match Two Columns with Two Other Columns
Remove Duplicates Column Combinations from a Dataframe in R
How to Capture the Output of System()
Using Proxy Interface in Plotly/Shiny to Dynamically Change Data
Testing a Function That Uses Enquo() for a Null Parameter
Significance Level Added to Matrix Correlation Heatmap Using Ggplot2
Harvest (Rvest) Multiple HTML Pages from a List of Urls
Contrasts Can Be Applied Only to Factor
Shiny Rcharts Multiple Chart Output
R Ggplot Ordering Bars in "Barplot-Like " Plot
Count Common Words in Two Strings
Error in Bind_Rows_(X, .Id):Argument 1 Must Have Names