I have faced a weird behaviour of geom_col and it drives me crazy: basically it does not plot all the data points. When I add geom_point() I clearly see that they are not all represented with a bar.
MWE :
x = sample(1:2000, size = 600, replace = FALSE)
y = 1:600
ggplot(data.frame(x = x, y = y), aes(x,y)) + geom_col() + geom_point()
It is actually plotting all vertical lines, the problem is that it is too thin to show. If you zoom in you will see all the lines.
Try to make the width of the lines thicker, e.g.
ggplot(data.frame(x = x, y = y), aes(x, y)) + geom_col(width = 3) + geom_point()
Related
I have a graph that shows a lot of information and I don't want to waste space on whitespace. Is there any way to decrease the plotted distance between points on the x axis. Ideally, I want them to almost touch. I can probably do that changing absolutely every size parameter (dots, axis labels, annotations, legend entries etc) in the plot to something huge but I was wondering if there is an easier way to do so along the lines of position.dodge or so?
Here is an example saved in two different sizes to show that they still have high relative distance:
library(ggplot)
plotdata <- data.frame(my_y = rnorm(3),
my_x = 1:3)
pdf("yourpath/test.pdf",
width = 4, height = 4)
ggplot(plotdata, aes(x = my_x, y = my_y)) + geom_point()
dev.off()
pdf("yourpath/test2.pdf",
width = 2, height = 2)
ggplot(plotdata, aes(x = my_x, y = my_y)) + geom_point()
dev.off()
My answer takes a similar approach to #Quinten but builds in a function for any data:
xmax<-max(plotdata$my_x);ymax=max(plotdata$my_y)
xmin<-min(plotdata$my_x);ymin=min(plotdata$my_y)
pdf("C:/temp/test.pdf",
width = 4, height = 4)
ggplot(plotdata, aes(x = my_x, y = my_y)) + geom_point()+
xlim(xmin-0.1,xmax+0.1)+
ylim(ymin-0.1,ymax+0.1)
dev.off()
If I understand you correctly, you can set limits in coord_cartesian. First plot without limits:
ggplot(plotdata, aes(x = my_x, y = my_y)) + geom_point()
Output:
With limits:
ggplot(plotdata, aes(x = my_x, y = my_y)) + geom_point() + coord_cartesian(xlim=c(-1, 3),
ylim=c(-1.5, 0.5))
Output:
As you can see, they are shown closer together.
I would like to plot a density plot using ggplot2, and make one section of the x-axis line thicker (or colored differently).
For example:
interval <- c(x1,x2)
x <- ggplot(df, aes(x=value)) + geom_density()
Is there any way to selectively make the x-axis segment corresponding to (x1,x2) thicker or colored differently? Thanks.
You can use annotate to add a line segment. Setting the y coordinates to -Inf will place it on the x axis. Since your example isn't reproducible, I've demonstrated on the mtcars data:
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() +
annotate(
geom = "segment",
x = 3, xend = 4,
y = -Inf, yend = -Inf,
color = "blue",
size = 5
)
I'm trying to add shapes on the lines plotted using geom_freqpoly to give more visibility to them if the plot is printed b/w on paper.
data <- data.frame(time=runif(1000,0,20000),
class=c("a","b","c","d"))
ggplot(data, aes(time, colour = class)) + geom_freqpoly(binwidth = 1000) + geom_point(aes(shape=class))
but this generates this error:
'Error: geom_point requires the following missing aesthetics: y'
How can I solve this error?
Another thing is that I want to use a single colour (eg. blue) to draw the lines
but with scale_colour_brewer() I can't change the colour scale, I want to change it because the lightest colour is nearly white and you can barely see it.
How can I add a custom min and max for the colours?
How about this? The error you are getting is being produced by geom_point which needs x and y, so I removed it.
ggplot(data, aes(x = time, color = class)) +
geom_freqpoly(binwidth = 1000) +
scale_color_brewer(palette = "Blues") +
theme_dark()
If you don't want the dark background, pass manual values from RColorBrewer. The following example uses every second color to increase the contrast.
p1 <- ggplot(data, aes(x = time, color = class)) +
geom_freqpoly(binwidth = 1000) +
scale_color_manual(values = RColorBrewer::brewer.pal(9, name = "Blues")[c(3, 5, 7, 9)])
EDIT
You can extract summarised data from a ggplot object using layer_data function.
xy <- layer_data(p1)
ggplot(xy, aes(x = x, y = count, color = colour)) +
theme_bw() +
geom_line() +
geom_point() +
scale_color_manual(values = RColorBrewer::brewer.pal(9, name = "Blues")[c(3, 5, 7, 9)])
I have a simple bar chart, and I want to set the Y axis labels in the middle position according to the correspondence fill value. I use scale_y_discrete to set the breaks and labels. However, only one label is shown. Can someone tell me what went wrong?
g = ggplot(mtcars, aes(x = factor(1), fill = factor(cyl))) + geom_bar(width = 1)
tmp = table(mtcars$cyl)
tmp = cumsum(tmp) - (tmp /2)
g + scale_y_discrete(breaks = tmp, labels = tmp)
In this plot, the scale of the y-axis is not discrete. Therefore, you have to use scale_y_continuous.
g + scale_y_continuous(breaks = tmp, labels = tmp)
Is there a way to plot both horizontal and vertical point ranges together on the same plot in ggplot. I understand that geom_pointrange(...) plots vertical point ranges, and that horizontal point ranges can be generated with coord_flip(...), but I'm interested in putting both together on the same plot.
set.seed(1)
df <- data.frame(x=sample(1:10,10),y=sample(1:10,10), x.range=1, y.range=2)
library(ggplot2)
ggplot(df) +
geom_pointrange(aes(x=x, y=y, ymin=y=y.range, ymax=y+y.range))
I'm looking for something like this:
ggplot(df) +
geom_pointrange(aes(x=x, y=y,
ymin=y-y.range, ymax=y+y.range,
xmin=x-x.range, xmax=x+x.range))
Which of course produces the same output as above because the xmin and xmax arguments are ignored. Evidently, there is (was) a function geom_hpointrange(...) in ggExtra, but this package has been pulled as far as I can tell.
Is geom_errorbarh what you are looking for?
ggplot(data = df, aes(x = x, y = y)) +
geom_pointrange(aes(ymin = y - y.range, ymax = y + y.range)) +
geom_errorbarh(aes(xmax = x + x.range, xmin = x - x.range, height = 0))
you can also call geompoint_range twice
ggplot(df, aes(x=x, y=y)) +
geom_pointrange(aes(ymin=y-y.range, ymax=y+y.range)) +
geom_pointrange(aes(xmin=x-x.range, xmax=x+x.range))