There is either a bug in ggplot or a bug in my brain.
The double green bar at height 27 should be a single line. It is defined by two x values and a single y value twice:
Even worse, if I expand the image to full screen more of these fly apart
Does anyone have any insight into this? Thanks.
ggplot() +
geom_line(data=plotData,
aes(x=x,y=y,group=interaction(y,type),color=color,
size=lineSize,linetype=lineType)) +
scale_color_manual(values=group.colors) +
ggtitle('EScells') +
xlab('chr2') +
ylab('')
Not surprisingly, the bug was in my brain. ggplot was treating the lines in question as dashed lines. Some of these dashed lines only became visible as dashes in the larger sized image.
Related
I am trying to plot with ggplot2 (v3.3.2) data points on a map using a specific projection (called aitoff), which is useful especially for sky plots.
When doing so, the plot is "cropped" on the x-axis, i.e. the edges of the axis are located just outside the plot. I tried a few things (adjust the margin for example), but without success. Could you please help to make these part of the plot visible?
Here is the code to reproduce the issue, i.e. the point located at (0,0) is not visible.
skydata <- data.frame(RA=c(0,180,360), Dec=c(0,10,20))
ggplot(skydata) +
geom_point(aes(RA,Dec)) +
coord_map(projection="aitoff",orientation=c(90,180,0)) +
scale_y_continuous(breaks=(-2:2)*30,limits=c(-90,90)) +
scale_x_continuous(breaks=(0:8)*45,limits=c(0,360), labels=c("","","","","","","","","")) +
labs(x="R.A.(°)", y="Decl. (°)",title="Map of the sky")
I hope I was clear enough...
Thanks a lot!
I think that the clipping happens in the first place is a known issue. In the Github issue hadley says: "I think this is a long standing problem that I'm unlikely to solve in the near future"
I think there are two ways, you can more or less solve the problem for yourself. One solution was already mentioned by #Arnaud
(but both solutions have downsides)
Add clip = "off" in the coord_map part
Add expand = c(1.1,0) in the scale_x_continuous part
I added you some example plots, where you can see the results and problems.
1. Initial version (if I run your code):
Problem: The point at (0,0) can't be seen properly.
2. Version with expand:
skydata <- data.frame(RA=c(0,180,360), Dec=c(0,10,20))
ggplot(skydata) +
geom_point(aes(RA,Dec)) +
coord_map(projection="aitoff", orientation=c(90,180,0)) +
scale_y_continuous(breaks=(-2:2)*30,limits=c(-90,90)) +
scale_x_continuous(breaks=(0:8)*45,limits=c(0,360), ,expand = c(1.1, 0), labels=c("","","","","","","","","")) +
labs(x="R.A.(°)", y="Decl. (°)",title="Map of the sky")
Looks quite nice now. x-axis got expanded to both sides, point in (0,0) now clearly visible.
But attention seems to work only with natural numbers (like expand = c(5,0)). For the 1.1 I chose in my example the plot is somehow different and the y-axis seems distorted.
3. Version with clip = "off":
skydata <- data.frame(RA=c(0,180,360), Dec=c(0,10,20))
ggplot(skydata) +
geom_point(aes(RA,Dec)) +
coord_map(projection="aitoff", clip = "off", orientation=c(90,180,0)) +
scale_y_continuous(breaks=(-2:2)*30,limits=c(-90,90)) +
scale_x_continuous(breaks=(0:8)*45,limits=c(0,360), labels=c("","","","","","","","","")) +
labs(x="R.A.(°)", y="Decl. (°)",title="Map of the sky")
This version does not expand the x-axis, but it makes sure, the point at (0,0) is not clipped off. Definitely no distortion of the y-axis. But does not look as good as the solution with expand.
I have a horizontal barchart, with too-tight padding:
data <- data.frame(month = factor(c("Nov", "Dec", "Jan", "Feb")),
count = c(1489, 788, 823, 1002))
g <- (ggplot2::ggplot(data, ggplot2::aes(x=month, y=count)) +
ggplot2::geom_col() +
ggplot2::scale_x_discrete(limits=rev(data$month)) +
ggplot2::coord_flip()) +
g
I am happy with the spacing between the bars, but I want a lot more space all around the 4-bar stack. In other words, I want more padding around the central 4-bar figure. The area of the light-gray rectangle that serves as the background for the 4-bar stack should remain unchanged, but the size of the 4-bar stack within that rectangle should shrink, with the net effect of produing wider padding all around the 4-bar stack; in other words, more of the light-gray background will be visible around the 4-bar stack.
Also, I am looking for ways to do this that are entirely independent of the actual values on the axes. The code should produce the same visual effect whether the x-range is 0-1500 or 0-1500000 or altogether non-numeric (i.e. categorical).
This means that the extra padding must be specified either (a) as percentages for the figure's total width and height; or (b) fixed numbers of pixels; or (c) fixed units of measurement (cm, inches, printer points, etc.).
Lastly, ideally, I would like to be able to specify the padding for all four edges independently.
How can I do this?
IMPORTANT I don't want to increase the margins around the light gray background. I want to increase the padding within the light gray background. If this distinction is not clear, please see this.
You can use the expand_scale function within the expand argument to scale_x_continuous-type functions. It's a bit wordy, but...
ggplot(data, aes(x=month,y=count)) +
geom_bar(stat="identity") +
scale_x_discrete(limits=(data$month), expand=expand_scale(mult=c(0.5,0.5))) +
geom_text(aes(label=count), hjust=-0.3) +
coord_flip() + scale_y_continuous(expand=expand_scale(mult=c(0.5,0.5)))
Play around with the two elements of the mult vector, which define the padding above and below the axis, so you can change each side of the plot independently (although it's not exactly transparent, and will take some fiddling). See ?expand_scale for more info.
I would like to reposition (e.g., move up by 10px) the x-axis label of this chart
ggplot() + geom_bar(aes(x=carb, y=..count..),data=mtcars)
I thought I could use vjust but as mentioned in this github issue, the only way to shift the x-label vertically is by using margin:
theme(axis.text.x = element_text(margin=margin(0,0,-10,0)))
However, margin produces something very different than a simple repositioning of the x-title, in that it resizes the chart too. I could I simply move the x-label instead?
You could do it this way
ggplot() + geom_bar(aes(x=carb, y=..count..),data=mtcars)+
theme(axis.title.x=element_text(margin=margin(-5,0,0,0)))
Just change the numbers until you get a satisfying result.
Is there any way to annotate a ggplot with three lines one on top of the other, while keeping the text sizes the same across the three lines?
I am almost there but the text sizes are different in the third line, it looks like the bgroup is only using the first two lines and I cannot get this right...
I am adding the text using to "atop" applications from "?plotmath", which works fine but the text in the third line comes out in a different size compared to the other two lines...
library(ggplot2)
line1 = "xxx data1"
line2 = "yyy data2"
line3 = "zzz data3"
df=data.frame(x=rep(1:8, 3), y=c(0,1,3,4,5,6,7,8, 8,7,6,3,2,1,3,4, 0,2,4,5,6,7,8,9), variable=c("x", "x","x","x","x","x","x","x","y","y","y","y","y","y","y","y","z","z","z","z","z","z","z","z"))
p <- ggplot(df) + theme_bw() + geom_point(aes(x=x,y=y, color=variable)) + geom_line(aes(x=x,y=y, color=variable)) +
geom_text(x=max(df$x), y = max(df$y), label = paste('bgroup("{", atop(atop("',line1,'","',line2,'"),"', line3,'"), "}")',sep=''), size=3.5,parse=TRUE)
I thought I was getting all the three lines within the bgroup, but I cannot get the paste right. If there is anything I can try please advise. Thanks very much!
To perfectly center everything (which \n will not do), keep every piece of text the same size whatever the number of lines and at the same time being able to adjust the interlinear space, use this instead:
xlab(expression(atop(textstyle("whateverline1"),atop(textstyle("whateverline2"),atop(scriptscriptstyle(""),textstyle("whateverline3"))))))
Then use labeller=label_parsed
This also works for facet_grid, title and ylab
Note the atop and textstyle to position the text whilst keeping it all the same size and the scriptscriptstyle("") to control spacing between lines. You can also use varied relative sizes of text using scriptstyle or scriptscriptstyle depending on your needs and of course use element_text(size=whatevernumber) in the theme section
I want to draw a bar plot, with ggplot and geom_bar, but it seems that the behavior of geom_bar is not consistent. I don't understand why.
My data is a time series of precipitations:
library(ggplot2)
library(data.table)
library(lubridate)
set.seed(42)
dt1 <- data.table(dateHeure=seq(ymd_hms("2014-06-04 13:30:00"),
ymd_hms("2014-10-20 08:30:00"), by='1 hour'),
rain=sample(c(rep(5,15), rep(10,15), rep(20,10),
rep(30, 5), 40, rep(0, 3262))))
Then i plot it, and not all the data appears... Why is some data missing?
ggplot(data=dt1)+
geom_bar(aes(x=dateHeure, y=rain),
stat="identity",
fill="blue") # doesn't work!
But if i add the variable color in aes, then the plot is correct!
ggplot(data=dt1)+
geom_bar(aes(x=dateHeure, y=rain, color="rain"),
stat="identity",
width=0.2) # work properly
So someone know why geom_bar doesn't work properly without color? Because i can't rely on it if sometimes not all the data is correctly plotted...
thanks!
edit: to respond to #eipi10, i added the plots. The strange thing is that when i resize the plot window in the first case, the data which is plotted changes!
Based on the edit to your question, I think I know what's happening: In the first plot, you use fill="blue". But the bin widths are very small compared to the overall range of the x-axis. This results in very, very thin vertical bars--so thin that you can't see some of them on your screen, but they appear when you expand the physical width of the plot.
On the other hand, in your second plot you used colour="rain", which adds a border to each bar, making each bar thicker, so they are visible, even when the physical width of the plot is relatively small.
Try adding colour="blue"(or "red" or whatever) to your first plot and I think you'll see all the bars, even without resizing. On the other hand, try changing colour="rain" to fill="rain" on your second plot and see if that creates the "disappearing data" effect on your second plot.
UPDATE: In response to your comment, you can use the colour parameter and then set the line width to get exactly the bar thickness you want, so you don't really need fill. For example:
ggplot(data=dt1)+
geom_bar(aes(x=dateHeure, y=rain),
stat="identity",
colour="blue", lwd=0.5)
Just set lwd (line width) to a value that gives you the bar-width you want. And, of course, you can also change the colour as well.