I'm stuck on an small labeling issue with a series of polar histograms made in ggplot2 (circumplexes? how are these things called?).
Here is a simplified example of how the data and the graph look:
df <- data.frame(Attribute1=10, Attribute2=1, Attribute3=2, Attribute4=6, Attribute5=7)
g <- ggplot(melt.data.frame(df), aes(x=variable, y=value, fill=variable, label=value))
g <- g + geom_bar() + geom_text() + coord_polar()
g
Which gives the following graph:
I would like to move the text labels outwards (away from the center).
Normally, I would adjust the position with hjust or vjust inside geom_text(), but it seems that, with coord_polar(), the result is to move all the labels up/downwards or left/rightwards, but not in/outwards.
This may sound trivial - and probably is - but I haven't found any applicable example or workaround yet, so I apologize if this question looks silly.
I'm assuming that you're referring to the numeric values as labels, and that you want them moved a little outside the pie wedges (as opposed to the "Attribute 1" text).
You can just move some of the aesthetic mapping to the geom_text call and add a small value to the y values:
g <- ggplot(melt.data.frame(df), aes(x=variable, y=value, fill=variable))
g <- g + geom_bar() + geom_text(aes(y = value + 0.5,label = value)) + coord_polar()
g
Related
I basically have a very simple question, how can I make bar charts shorter? If I search for it on the web I can only find "width", but this is not what I want. I want to make the lenght of the bars shorter.
library(ggplot2)
# data
data <- data.frame(
name=c("A","B","C","D","E") ,
value=c(3,12,5,18,45)
)
# bar chart
ggplot(data, aes(x=name, y=value)) +
geom_bar(stat = "identity", width=0.8) +
coord_flip()
I basically just want that the lenght of the bar goes up to like around 20, so somehow just half the length..
I think you mean how do you make the bars visibly shorter on the page without affecting their numerical representation. Perhaps you just need to set the axis limits?
If your plot looks like this...
p <- ggplot(data, aes(x=name, y=value)) +
geom_bar(stat = "identity", width=0.8) +
coord_flip()
p
Then you can just do this...
p + scale_y_continuous(limits = c(0, 2 * max(data$value)))
Created on 2020-11-10 by the reprex package (v0.3.0)
I'm hoping if there's a way to remove whitespace in one side of the panel plot (created by facet_wrap) by adding "//" on the x-axis. Below is sample data and code:
df <- data.frame(
condition = c("cond1","cond2","cond3"),
measure = c("type1","type2"),
value = rep(NA, 6)
)
# all type 1 measure values are between -0.5 and 0.5
# all type 2 measure values are between 0.5 and 2
df[df$measure=="type1",]$value <- runif(3, min=-0.5, max=0.5)
df[df$measure=="type2",]$value <- runif(3, min= 1.5, max=2.0)
# both panels should have same axis tick intervals
custom_breaks = function(x){
seq(round(min(x), 2), round(max(x), 2), 0.2)
}
# create a panel plot with vertical line at y=0 for both panels
ggplot(df, aes(x=condition, y=value, color=measure)) +
geom_point() +
geom_hline(aes(yintercept=0), color="grey") +
scale_y_continuous(breaks=custom_breaks) +
facet_wrap(~measure, scales="free_x") +
coord_flip() +
theme_bw() +
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank())
This code returns the below plot:
Because the values for type 2 (right panel) are far off from zero, adding a vertical line at y=0 results in lots of whitespace. I'm wondering if there's a way to put a "//" on the x-axis on the right panel after 0 and going straight to 1.5 so there aren't tons of wasted white space. Any help would be greatly appreciated!
Broken axes are generally discouraged because they can lead to misleading visualizations, so this is intentionally not implemented in ggplot2 (as answered by Hadley Wickham himself).
My preferred solutions for something like this are (a) facetting (which you are already doing) or (b) log transormation of the axis - but only if it makes sense for the given data.
Take this barchart for example (source / link to image): Since there is valuable information in the outliers (red circle and arrows) both log transformation and broken axes would distort the representation of reality. The package library(ggforce) has an implementation for such zoom facets with the facet_zoom() function.
Your scales = "free_x" is working just fine - the issue is that your geom_hline putting a line at 0 is included in both facets. Here's a way to include it only on the first facet.
ggplot(df, aes(x=condition, y=value, color=measure)) +
geom_point() +
geom_hline(data = data.frame(measure = "type1"), aes(yintercept=0), color="grey") +
scale_y_continuous(breaks=custom_breaks) +
facet_wrap(~measure, scales="free_x") +
coord_flip() +
theme_bw() +
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank())
Is it possible to add horizontal lines from 0 to the points on the plot shown below?
This is the code thus far:
ggplot(data, aes(x=change, y=industry, color=geo)) + geom_point() +
scale_x_continuous(labels = scales::comma) + geom_vline(xintercept = 0)
Alternatively, I could use geom_bar() but I have been unsure how to show both London and the UK without them summing together.
tl;dr you can use geom_bar() with position="stack", stat="identity". Or you can use geom_segment().
set up data
dd <- expand.grid(industry=c("property",
"manufacturing",
"other"),
geo=c("London","UK"))
set.seed(101)
dd$change <- runif(6,min=-30,max=30)
This is how you could do it with geom_bar
library(ggplot2)
ggplot(dd,aes(x=industry,y=change,
fill=geo))+
geom_bar(stat="identity",
position="dodge")+
coord_flip()
Or with geom_segment():
ggplot(dd,aes(x=change,y=industry,
colour=geo))+
geom_point(size=2)+
geom_segment(aes(xend=0,yend=industry))
You might want to consider manually dodging the position in the second case, but position_dodge in ggplot can only dodge horizontally, so you should either switch x and y and use coord_flip(), or use position_dodgev from the ggstance package.
I want to add a legend for filled rectangles in the background but I already used fill aesthetics for filling the bars of my bar plot.
How can I get the legend or create a matching legend by hand?
df <- data.frame(a=factor(c('a','b','c','c','d','e'), levels=c('a','b','c','d','e')),
x=seq(1,6),
b=factor(c('A','A','A','B','B','B'), levels=c('A','B')),
c=c(1,2,3,4,5,6),
d=rnorm(6))
ggplot(df, aes(x, c, fill=d, group=b)) +
geom_rect(aes(xmin=0.5,xmax=3.5,ymin=-Inf,ymax=Inf),alpha=0.05,fill="#E41A1C") +
geom_rect(aes(xmin=3.5,xmax=6.5,ymin=-Inf,ymax=Inf),alpha=0.05,fill="#377EB8") +
geom_bar(stat='identity', position=position_dodge()) +
coord_flip() +
scale_x_continuous(breaks=df$x, labels=df$a)
So I need a legend describing my two geom_rect areas. I was not able to map my two areas in any way to get a legend. In general the column df$b is describing the areas I do now by hand.
You can set colour= to variable b inside the aes() of both geom_rect(). This will make lines around the rectangles and also make legend. Lines can be removed setting size=0 for geom_rect(). Now using guides() and override.aes= you can change fill= for legend key.
ggplot(df, aes(x, c, fill=d, group=b)) +
geom_rect(aes(xmin=0.5,xmax=3.5,ymin=-Inf,ymax=Inf,colour=b),alpha=0.05,fill="#E41A1C",size=0) +
geom_rect(aes(xmin=3.5,xmax=6.5,ymin=-Inf,ymax=Inf,colour=b),alpha=0.05,fill="#377EB8",size=0) +
geom_bar(stat='identity', position=position_dodge()) +
coord_flip() +
scale_x_continuous(breaks=df$x, labels=df$a)+
guides(colour=guide_legend(override.aes=list(fill=c("#E41A1C","#377EB8"),alpha=0.3)))
I'm drawing some line segments on top of a plot that uses geom_line(). Surprisingly, the guide (legend) colors for geom_line() are drawn as the color of the last element I add to the plot - even if it is not the geom_line(). This looks like a bug to me, but it could be expected behavior for some reason I don't understand.
#Set up the data
require(ggplot2)
x <- rep(1:10, 2)
y <- c(1:10, 1:10+5)
fac <- gl(2, 10)
df <- data.frame(x=x, y=y, fac=fac)
#Draw the plot with geom_segment second, and the guide is the color of the segment
ggplot(df, aes(x=x, y=y, linetype=fac)) +
geom_line() +
geom_segment(aes(x=2, y=7, xend=7, yend=7), colour="red")
Whereas if I add the geom_segment first, the colors on the guide are black as I would expect:
ggplot(df, aes(x=x, y=y, linetype=fac)) +
geom_segment(aes(x=2, y=7, xend=7, yend=7), colour="red") +
geom_line()
Feature or bug? If the first, can someone explain what's happening?
Feature(ish). The guide that is drawn is a guide for linetype. But, it has to be drawn in some color to be seen. When the color is not specified by an aesthetic mapping, ggplot2 draws it in a color that is consistent with the plot. I'm speculating that the default is whatever last color was used. That is why you are seeing differences when you plot them in a different order.
However, you can control these details of the legend.
ggplot(df, aes(x=x, y=y, linetype=fac)) +
geom_line() +
geom_segment(aes(x=2, y=7, xend=7, yend=7), colour="red") +
scale_linetype_discrete(guide=guide_legend(override.aes=aes(colour="blue")))