I'd like to produce an area/bar graph in R similar to this:
(plot from David MacKay's (excellent) book "Sustainable Energy")
I honestly can't even find the proper name for a plot like this. It seems to be a bar graph with variable width bars. Certainty a powerful communication tool.
You can do this with base graphics. First we specify some widths and heights:
widths = c(0.5, 0.5, 1/3,1/4,1/5, 3.5, 0.5)
heights = c(25, 10, 5,4.5,4,2,0.5)
Then we use the standard barplot command, but specify the space between blocks to be zero:
##Also specify colours
barplot(heights, widths, space=0,
col = colours()[1:6])
Since we specified widths, we need to specify the axis labels:
axis(1, 0:6)
To add grid lines, use the grid function:
##Look at ?grid to for more control over the grid lines
grid()
and you can add arrows and text manually:
arrows(1, 10, 1.2, 12, code=1)
text(1.2, 13, "A country")
To add your square in the top right hand corner, use the polygon function:
polygon(c(4,4,5,5), c(20, 25, 25, 20), col="antiquewhite1")
text(4.3, 22.5, "Hi there", cex=0.6)
This all gives:
Aside: in the plot shown, I've used the par command to adjust a couple of aspects:
par(mar=c(3,3,2,1),
mgp=c(2,0.4,0), tck=-.01,
cex.axis=0.9, las=1)
Inspired by the code from the blog post I mentioned above,
df <- data.frame(x = c("Alpha", "Beta", "Gamma", "Delta"), width = c(25, 50, 75, 100), height = c(100, 75, 50, 25))
df$w <- cumsum(df$width)
df$wm <- df$w - df$width
df$wt <- with(df, wm + (w - wm)/2)
library(ggplot2)
p <- ggplot(df, aes(ymin = 0))
p1 <- p + geom_rect(aes(xmin = wm, xmax = w, ymax = height, fill = x))
library(grid) # needed for arrow function
p1 + geom_text(aes(x = wt, y = height * 0.8, label = x)) +
theme_bw() + labs(x = NULL, y = NULL) +
theme(axis.ticks = element_blank(),axis.text.x = element_blank(),
axis.text.y = element_blank(), legend.position = "none") +
annotate("text", x = 120, y = 83, label = "a Beta block") +
geom_segment(aes(x = 100, y = 80, xend = 80, yend = 75),
arrow = arrow(length = unit(0.5, "cm")))
Related
How to get ggplot Heatmap (R) to use two colors? One for between a fill value of -.1 to .1 and one for not
ggplot(base, aes(x,y, fill= base$`Equal Opportunity Difference`)) +
geom_tile() +
#axis formatting
scale_x_discrete(breaks = c(10, 20, 30, 40 , 50, 60, 70, 80, 90),
labels = c("10%", "20%","30%", "40%","50%", "60%", "70%","80%", "90%"),
limits = c(10,90)) +
scale_y_discrete(breaks = c(10, 20, 30, 40 , 50, 60, 70, 80, 90),
labels = c("10%", "20%","30%", "40%","50%", "60%", "70%","80%", "90%"),
limits = c(10,90)) +
geom_text(aes(label = signif(base$`Equal Opportunity Difference`,2)), color = "white",
size = 4) +
scale_fill_gradient2(midpoint=c(-.1, s.1), low="#B2182B", high="#2166AC")
This is what I have right now, which isn't working. Also the axis are only showing 10% and 90%
I would appreciate if someone had a solution for that too.
Without a minimal reproducible example it's difficult to guess potential solutions to your problem. Is this what you're hoping to achieve? If not, what do you want to change?
library(ggplot2)
library(scales)
x <- seq(1:10)
y <- seq(1:10)
df <- expand.grid(x = x, y = y)
df$z <- signif(c(runif(50, -10, 0), runif(50, 0, 10)), 2)
df$z_categorised <- cut(df$z, c(seq(-10, -1, 1), seq(1, 10, 1)))
palette_red_blue <- colorRampPalette(colors = c("#B2182B","white", "#2166AC"))
ggplot(df, aes(x = x, y = y, fill = z_categorised)) +
geom_tile(color = "white") +
geom_text(aes(label = z)) +
scale_fill_manual(values = palette_red_blue(19)) +
scale_x_continuous(breaks = seq(0, 10, 1),
labels = percent_format(scale = 10)) +
scale_y_continuous(breaks = seq(0, 10, 1),
labels = percent_format(scale = 10)) +
coord_cartesian(expand = 0)
Created on 2022-06-21 by the reprex package (v2.0.1)
I'm trying to slightly reposition the labels of a discrete colorbar so that they don't overlap, without changing the values of the breaks themselves. In the below plot, the two center labels (bracketing the near-zero data) are too close together, so that it looks like '-11' instead of '-1' and '1'. I'd like to nudge them to either side, or change the justification of each half of the scale (left justify the negatives and right justify the positives), or anything to create more space between the labels while retaining the spacing of the actual colorbar. (Making the colorbar wider is not an option in my actual figure.)
Here is the code used to create this plot:
library(dplyr)
library(ggplot2)
library(scales)
df <- data.frame(
x = runif(1000),
y = runif(1000),
z1 = rnorm(100)*10
)
df %>% ggplot() +
geom_point(aes(x=x,y=y, color=z1)) +
scale_color_steps2(low = muted("darkblue"), mid = "white", high = muted("darkred"),
midpoint = 0, guide_colorbar(barwidth = 20),
breaks = c(-20, -10, -5, -1, 1, 5, 10, 20)) +
theme_minimal() +
theme(legend.position = 'bottom') +
labs(x='', y='', color='')
Always a bit hacky and you get a warning but one option would be to pass a vector to hjust argument of element_text to align the -1 to the right and the 1 to the left:
library(ggplot2)
set.seed(123)
df <- data.frame(
x = runif(1000),
y = runif(1000),
z1 = rnorm(100)*10
)
ggplot(df) +
geom_point(aes(x=x,y=y, color=z1)) +
scale_color_steps2(low = scales::muted("darkblue"), mid = "white", high = scales::muted("darkred"),
midpoint = 0, guide = guide_colorbar(barwidth = 20),# horizontal_legend,
breaks = c(-20, -10, -5, -1, 1, 5, 10, 20)) +
theme_minimal() +
theme(legend.position = 'bottom') +
labs(x='', y='', color='') +
theme(legend.text = element_text(hjust = c(rep(.5, 3), 1, 0, rep(.5, 3))))
#> Warning: Vectorized input to `element_text()` is not officially supported.
#> Results may be unexpected or may change in future versions of ggplot2.
I made a plot for presenting OR ratio. However, OR for log(PTH) has a large 95CI. Can I make a gap into x axis between 20 and 30, making other variables more visible? Some, examples suggest gap_plot(), but I do not know how to combine it with this type of graph.
I already transform PTH value, so it will be hard to change it regarding interpretation. If you have some other way to do it, feel free to suggest. I would like to make values of all variables visible. However, this vales of lof(PTH) makes Pol hard to interpret from the plot, despite being significant.
Best,
A.
library(gridExtra)
library(ggplot2)
Nezavisna<-c("Pol","Starost","Ca","P","log(PTH)","log(mg)","BrojZlezda","MIBI","Iskustvo","Pridruzena")
OR<-c(0.399,1.023,0.814,0.568,14.14,0.417,2.193,0.709,1.468,1.445)
LL<-c(0.174,0.996,0.277,0.122,4.969,0.197,1.019,0.339,0.754,0.702)
UL<-c(0.917,1.052,2.396,2.649,40.238,0.882,4.722,1.483,2.859,2.976)
istrazivanje<-data.frame(Nezavisna,OR,LL,UL)
istrazivanje
ggplot(istrazivanje, aes(y = Nezavisna, x = OR)) +geom_point(shape = 18, size = 5) + geom_errorbarh(aes(xmin = LL, xmax = UL), height = 0.25) + geom_vline(xintercept = 1, color = "red", linetype = "dashed", cex = 1, alpha = 0.5)
As mentioned by stefan in the comments you can transform your data, but to make it readable set your labels and breaks to the normal values.
ggplot(istrazivanje, aes(y = Nezavisna, x = log2(OR))) +
geom_point(shape = 18, size = 5) +
geom_errorbarh(aes(xmin = log2(LL), xmax = log2(UL)), height = 0.25) +
geom_vline(xintercept = log2(1), color = "red", linetype = "dashed", cex = 1, alpha = 0.5) +
scale_x_continuous(breaks = log2(c(1, 10, 20, 30, 40)), labels =c(1, 10, 20, 30, 40))
Edit: or as you already take log(PTH) you could do log10(PTH) which roughly bring your OR value there from roughly 14 to 6 and perhaps then you do not even have to transform the scales of the graph.
If, I understand correctly your question one solution could be ggforce facet zoom function
Sample code:
library(gridExtra)
library(ggplot2)
library(ggforce)
ggplot(istrazivanje, aes(y = Nezavisna, x = OR)) +
geom_point(shape = 18, size = 5) +
geom_errorbarh(aes(xmin = LL, xmax = UL), height = 0.25) +
geom_vline(xintercept = 1,
color = "red",
linetype = "dashed",
cex = 1,
alpha = 0.5)+
facet_zoom(xlim = c(20, 30)) # use facet_zoom to zoom on x axis
Plot:
Sample data:
Nezavisna<-c("Pol","Starost","Ca","P","log(PTH)","log(mg)","BrojZlezda","MIBI","Iskustvo","Pridruzena")
OR<-c(0.399,1.023,0.814,0.568,14.14,0.417,2.193,0.709,1.468,1.445)
LL<-c(0.174,0.996,0.277,0.122,4.969,0.197,1.019,0.339,0.754,0.702)
UL<-c(0.917,1.052,2.396,2.649,40.238,0.882,4.722,1.483,2.859,2.976)
istrazivanje<-data.frame(Nezavisna,OR,LL,UL)
istrazivanje
I have 6 plots which I want to align neatly in a two-step manner (see picture). Preferably, I'd like to add nice arrows.
Any ideas?
UPD. As my question started to gather negative feedback, I want to clarify that I've checked all the (partially) related questions at SO and found no indication on how to position ggplots freely on a "canvas". Moreover, I cannot think of a single way to draw arrows between the plots. I'm not asking for a ready made solution. Please, just indicate the way.
Here's an attempt at the layout you want. It requires some formatting by hand, but you can probably automate much of that by taking advantage of the coordinate system built into the plot layout. Also, you may find that grid.curve is better than grid.bezier (which I used) for getting the arrow curves shaped exactly the way you want.
I know just enough about grid to be dangerous, so I'd be interested in any suggestions for improvements. Anyway, here goes...
Load the packages we'll need, create a couple of utility grid objects, and create a plot to lay out:
library(ggplot2)
library(gridExtra)
# Empty grob for spacing
#b = rectGrob(gp=gpar(fill="white", col="white"))
b = nullGrob() # per #baptiste's comment, use nullGrob() instead of rectGrob()
# grid.bezier with a few hard-coded settings
mygb = function(x,y) {
grid.bezier(x=x, y=y, gp=gpar(fill="black"),
arrow=arrow(type="closed", length=unit(2,"mm")))
}
# Create a plot to arrange
p = ggplot(mtcars, aes(wt, mpg)) +
geom_point()
Create the main plot arrangement. Use the empty grob b that we created above for spacing the plots:
grid.arrange(arrangeGrob(p, b, p, p, heights=c(0.3,0.1,0.3,0.3)),
b,
arrangeGrob(b, p, p, b, p, heights=c(0.07,0.3, 0.3, 0.03, 0.3)),
ncol=3, widths=c(0.45,0.1,0.45))
Add the arrows:
# Switch to viewport for first set of arrows
vp = viewport(x = 0.5, y=.75, width=0.09, height=0.4)
pushViewport(vp)
#grid.rect(gp=gpar(fill="black", alpha=0.1)) # Use this to see where your viewport is located on the full graph layout
# Add top set of arrows
mygb(x=c(0,0.8,0.8,1), y=c(1,0.8,0.6,0.6))
mygb(x=c(0,0.6,0.6,1), y=c(1,0.4,0,0))
# Up to "main" viewport (the "full" canvas of the main layout)
popViewport()
# New viewport for lower set of arrows
vp = viewport(x = 0.6, y=0.38, width=0.15, height=0.3, just=c("right","top"))
pushViewport(vp)
#grid.rect(gp=gpar(fill="black", alpha=0.1)) # Use this to see where your viewport is located on the full graph layout
# Add bottom set of arrows
mygb(x=c(1,0.8,0.8,0), y=c(1,0.9,0.9,0.9))
mygb(x=c(1,0.7,0.4,0), y=c(1,0.8,0.4,0.4))
And here's the resulting plot:
Probably using ggplot with annotation_custom here is a more convenient approach. First, we generate sample plots.
require(ggplot2)
require(gridExtra)
require(bezier)
# generate sample plots
set.seed(17)
invisible(
sapply(paste0("gg", 1:6), function(ggname) {
assign(ggname, ggplotGrob(
ggplot(data.frame(x = rnorm(10), y = rnorm(10))) +
geom_path(aes(x,y), size = 1,
color = colors()[sample(1:length(colors()), 1)]) +
theme_bw()),
envir = as.environment(1)) })
)
After that we can plot them inside a bigger ggplot.
# necessary plot
ggplot(data.frame(a=1)) + xlim(1, 20) + ylim(1, 32) +
annotation_custom(gg1, xmin = 1, xmax = 9, ymin = 23, ymax = 31) +
annotation_custom(gg2, xmin = 11, xmax = 19, ymin = 21, ymax = 29) +
annotation_custom(gg3, xmin = 11, xmax = 19, ymin = 12, ymax = 20) +
annotation_custom(gg4, xmin = 1, xmax = 9, ymin = 10, ymax = 18) +
annotation_custom(gg5, xmin = 1, xmax = 9, ymin = 1, ymax = 9) +
annotation_custom(gg6, xmin = 11, xmax = 19, ymin = 1, ymax = 9) +
geom_path(data = as.data.frame(bezier(t = 0:100/100, p = list(x = c(9, 10, 10, 11), y = c(27, 27, 25, 25)))),
aes(x = V1, y = V2), size = 1, arrow = arrow(length = unit(.01, "npc"), type = "closed")) +
geom_path(data = as.data.frame(bezier(t = 0:100/100, p = list(x = c(9, 10, 10, 11), y = c(27, 27, 18, 18)))),
aes(x = V1, y = V2), size = 1, arrow = arrow(length = unit(.01, "npc"), type = "closed")) +
geom_path(data = as.data.frame(bezier(t = 0:100/100, p = list(x = c(15, 15, 12, 9), y = c(12, 11, 11, 11)))),
aes(x = V1, y = V2), size = 1, arrow = arrow(length = unit(.01, "npc"), type = "closed")) +
geom_path(data = as.data.frame(bezier(t = 0:100/100, p = list(x = c(15, 15, 12, 9), y = c(12, 11, 11, 9)))),
aes(x = V1, y = V2), size = 1, arrow = arrow(length = unit(.01, "npc"), type = "closed")) +
geom_path(data = as.data.frame(bezier(t = 0:100/100, p = list(x = c(15, 15, 12, 12), y = c(12, 10.5, 10.5, 9)))),
aes(x = V1, y = V2), size = 1, arrow = arrow(length = unit(.01, "npc"), type = "closed")) +
theme(rect = element_blank(),
line = element_blank(),
text = element_blank(),
plot.margin = unit(c(0,0,0,0), "mm"))
Here we use bezier function from bezier package to generate coordinates for geom_path. Maybe one should look for some additional information about bezier curves and their control points to make connections between plots look prettier. Now the resulting plot is following.
Thanks a lot for your tips and especially #eipi10 for an actual implementation of them - the answer is great.
I found a native ggplot solution which I want to share.
UPD While I was typing this answer, #inscaven posted his answer with basically the same idea. The bezier package gives more freedom to create neat curved arrows.
ggplot2::annotation_custom
The simple solution is to use ggplot's annotation_custom to position the 6 plots over the "canvas" ggplot.
The script
Step 1. Load the required packages and create the list of 6 square ggplots. My initial need was to arrange 6 maps, thus, I trigger theme parameter accordingly.
library(ggplot2)
library(ggthemes)
library(gridExtra)
library(dplyr)
p <- ggplot(mtcars, aes(mpg,wt))+
geom_point()+
theme_map()+
theme(aspect.ratio=1,
panel.border=element_rect(color = 'black',size=.5,fill = NA))+
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
labs(x = NULL, y = NULL)
plots <- list(p,p,p,p,p,p)
Step 2. I create a data frame for the canvas plot. I'm sure, there is a better way to this. The idea is to get a 30x20 canvas like an A4 sheet.
df <- data.frame(x=factor(sample(1:21,1000,replace = T)),
y=factor(sample(1:31,1000,replace = T)))
Step 3. Draw the canvas and position the square plot over it.
canvas <- ggplot(df,aes(x=x,y=y))+
annotation_custom(ggplotGrob(plots[[1]]),
xmin = 1,xmax = 9,ymin = 23,ymax = 31)+
annotation_custom(ggplotGrob(plots[[2]]),
xmin = 13,xmax = 21,ymin = 21,ymax = 29)+
annotation_custom(ggplotGrob(plots[[3]]),
xmin = 13,xmax = 21,ymin = 12,ymax = 20)+
annotation_custom(ggplotGrob(plots[[4]]),
xmin = 1,xmax = 9,ymin = 10,ymax = 18)+
annotation_custom(ggplotGrob(plots[[5]]),
xmin = 1,xmax = 9,ymin = 1,ymax = 9)+
annotation_custom(ggplotGrob(plots[[6]]),
xmin = 13,xmax = 21,ymin = 1,ymax = 9)+
coord_fixed()+
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0)) +
theme_bw()
theme_map()+
theme(panel.border=element_rect(color = 'black',size=.5,fill = NA))+
labs(x = NULL, y = NULL)
Step 4. Now we need to add the arrows. First, a data frame with arrows' coordinates is required.
df.arrows <- data.frame(id=1:5,
x=c(9,9,13,13,13),
y=c(23,23,12,12,12),
xend=c(13,13,9,9,13),
yend=c(22,19,11,8,8))
Step 5. Finally, plot the arrows.
gg <- canvas + geom_curve(data = df.arrows %>% filter(id==1),
aes(x=x,y=y,xend=xend,yend=yend),
curvature = 0.1,
arrow = arrow(type="closed",length = unit(0.25,"cm"))) +
geom_curve(data = df.arrows %>% filter(id==2),
aes(x=x,y=y,xend=xend,yend=yend),
curvature = -0.1,
arrow = arrow(type="closed",length = unit(0.25,"cm"))) +
geom_curve(data = df.arrows %>% filter(id==3),
aes(x=x,y=y,xend=xend,yend=yend),
curvature = -0.15,
arrow = arrow(type="closed",length = unit(0.25,"cm"))) +
geom_curve(data = df.arrows %>% filter(id==4),
aes(x=x,y=y,xend=xend,yend=yend),
curvature = 0,
arrow = arrow(type="closed",length = unit(0.25,"cm"))) +
geom_curve(data = df.arrows %>% filter(id==5),
aes(x=x,y=y,xend=xend,yend=yend),
curvature = 0.3,
arrow = arrow(type="closed",length = unit(0.25,"cm")))
The result
ggsave('test.png',gg,width=8,height=12)
I have data with continuous x and y values. Over a specific x interval, I want to make the ticks increments to be smaller, e.g. from 50 to 60, the distance between the breaks should be 1 (50, 51, 52, 53 ... 59, 60). For the rest of the axis, it is fine to have the ticks incremented by 10. My desired x-axis would have breaks at:
10,20,30,40,50,51,52,53,54,55,56,57,58,58,60,70,80,90,..190,200
What I have tried:
x <- seq(1:200)
y <- seq(51, 250, by = 1)
df <- data.frame(x = x, y = y)
ggplot(data = df, aes(x, y)) +
geom_line(size=1.6)+
scale_x_continuous(breaks = c(10, 20, 30, 40, seq(50, 60, by = 2), seq(70, 200, 10)),
minor_breaks = seq(50, 60, by = 2)) +
theme(axis.text.x = element_text(size = 16),
axis.text.y = element_text(size = 16),
axis.title.x = element_text(size = 16),
axis.title.y = element_text(size = 16),
axis.ticks.x = element_line(size = 1),
axis.ticks.length = unit(0.8, "cm")) +
xlab("Time") + ylab("value")+
As you see, the labels are overlapping. How can I achieve this in a clearer way?
It seems very tight to squeeze in more labels than every 10. So you may try to drop the labels at tickmark 52 to 58, by labelling these four positions with ""
ggplot(data = df, aes(x = x, y = y)) +
geom_line() +
scale_x_continuous(breaks = c(seq(from = 10, to = 200, by = 10),
seq(from = 52, to = 58, by = 2)),
labels = c(seq(from = 10, to = 200, by = 10), rep("", 4)))
Alternatively, you can zoom in on the relevant x-range using coord_cartesian. The underlying data is unchanged, and we just magnify a small section of the original data. The zoomed-in plot can then be added to the original plot as a subplot. There are many ways to arrange subplots. Here is one example:
# The original plot on full range of x
g1 <- ggplot(data = df, aes(x = x, y = y)) +
geom_line()
# zoom in to the relevant section of x
g2 <- ggplot(data = df, aes(x = x, y = y)) +
geom_line() +
coord_cartesian(xlim = c(49, 61)) +
scale_x_continuous(breaks = seq(from = 50, to = 60, by = 2))
# print g1, and then add g2 on top using viewport from package grid
g1
print(g2, vp = viewport(x = 0.75, y = 0.3, width = 0.35, height = 0.35))