At some release, the dots of geom_point became bigger. It might be 2.0: "geom_point() now uses shape 19 instead of 16."
How can I make the default dot from geom_point smaller like it used to be?
Edit: How do I change ALL plots without adding code to every plot? That is, the default. I looked in get_theme() and didn't see anything about points.
refer http://ggplot2.tidyverse.org/reference/geom_point.html
ggplot(mtcars, aes(disp, hp)) + geom_point(shape = 16)
For updating default ggplot geom parameters:
update_geom_defaults("point", list(shape = 16))
Related
I have a ggplot object. I want to use annotate() to add a label to the top of the plot, so that the upper edge of the label is also the upper edge of the plot. When using default settings, this doesn't seem possible: adding an annotation at the upper edge of the plot causes the upper y-limit to increase.
One can get around this problem by specifying scale_y_continuous(expand = c(0, 0)) when creating the plot. But I don't want to do that, partly because I like the y limits created by the default expand setting. Given this constraint, is it possible to use annotate() to position a label at the top of the plot?
Here is a minimal example that demonstrates the problem:
library(ggplot2)
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
yMax <- layer_scales(p)$y$range$range[2] # upper y-limit
p + annotate("label", x = 30, y = yMax, vjust = "top", label = "X")
And here is the result:
You see that the annotation is not at the top of the plot. Instead, consistent with the default "expand" settings, the y-limit of the plot has changed.
Possible solutions:
Figure out the y-limits implied by the default expand setting. Then use scale_y_continuous() to both set the y limits and set expand = c(0, 0). This solution will give me the y limits that I want, and it will place the label appropriately. I know how to implement it, but it seems a bit cumbersome. It would also prevent other annotations at the top of the figure from changing the y-limit of the plot -- and I don't want the solution to affect annotations other than the one that I describe here.
Use annotation_custom(), which doesn't change plot limits in the same way. #baptiste suggests a solution like that in this answer to a different question. But annotation_custom() requires a grob. In practice, the annotations that I use may be more complicated than the label in this example, and I won't always know how to create them as a grob that can be passed to annotation_custom(). In addition, I've had some trouble positioning grobs with annotation_custom() while also specifying their exact sizes.
That said, I am quite open to annotation_custom()-based solutions. And perhaps there are solutions other than the two that I've sketched above.
I've read many SO posts on changing plot limits, but I haven't found any that speak to this problem.
A simple solution for that is setting y = Inf instead of using the maximum value found of the y-axis (yMax). The code would be like that then:
# load library
library(ggplot2)
# load data
data(mtcars)
# define plot
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p + annotate("label", x = 30, y = Inf, vjust = "top", label = "X")
Here is the output:
Let me know if this is what you're looking for.
Does this help?
library(ggplot2)
data(mtcars)
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
geom_text(label = "X", x = 30, y = max(mtcars$wt))
I'm making many plots and want to set a default color for the data labels without having to pass the color argument to every geom_text call. I can do it for the plot titles and axes, but not the data labels.
# Example of how to set default color for other text elements
library(ggplot2)
theme_set(theme_bw() + theme(text = element_text(color = "red"),
axis.text = element_text(color = "red")))
ggplot(mtcars, aes(x = cyl, label = ..count..)) +
geom_bar() +
geom_text(stat = "count") +
labs(title = "title")
Unfortunately, I believe theme elements are only intended to apply to non-data-related elements of the plot, meaning the theme does not cover the text in geom_text. The default color "black" is hard-coded in the source of geom_text, so as far as I know, there's not a simple way to override it. (Though, if someone cares to correct me, excellent!)
However, one simple solution that may help streamline things is to create a wrapper function that will return a geom_text with all the defaults that you will be passing over and over. For example:
geom_text_wrap <- function(col="red", ...) {
geom_text(col=col, ...)
}
can be used in place of geom_text directly, and will, by default, create red text. So the following will create red text without you having to specify it directly in the plot creation.
ggplot(mtcars, aes(x = cyl, label = ..count..)) +
geom_bar() +
geom_text_wrap(stat="count") +
labs(title = "title")
Note: If you really are creating a ton of similar plots to the point that you are tiring of specifying repetitive arguments, you may consider writing a function that will create the complete graph objects programmatically. That will depend on your specific use-case.
Is there any option to use two variables in parameter size in ggplot function?
E.g.
ggplot(mtcars, aes(qsec,drat, size = cyl))+
geom_point()
Obviously it works and size of points depends on cyl variable. But is it any option to add another size variable which can use alpha option e.g. variable = mpg. How to do it?
I did it just for three dots as an example in Ilustrator.
Thanks,
Update
Thanks to #lukeA following lines of code works:
ggplot() +
geom_point(data = mtcars, aes(qsec,drat, size = cyl)) +
geom_point(data = mtcars, aes(qsec,drat, size = mpg), alpha = .1, colour = "red")
But when I want to set the size of each variable separately, it is not possible. When there is one size variable normally I use scale_size_contiunous but with two size variables it doesn't work. I know that using scale_size_continuous two times doesn't change anything. Maybe it is not possible at all, but maybe somebody find the solution.
You can do this now with ggnewscale - allowing you to have more than one scale for any aesthetic. The scales can be defined separately too. Here's an example:
library(ggplot2)
library(ggnewscale)
ggplot(mtcars, aes(x=qsec, y=drat)) + theme_bw() +
geom_point(aes(size=cyl), color='gray20') +
scale_size_continuous(range=c(0.2, 3)) +
new_scale("size") +
geom_point(aes(size=mpg), alpha=0.1, color='red') +
scale_size_continuous(range=c(5, 10))
You can define the ranges for each scale separately. In this case, that's important so that you can ensure that the range of sizes mapped to mpg is larger than the range of sizes mapped to cyl. Otherwise, both are mapped to the default range (c(1,6)), which means many points would not have a visible red point.
I am trying to change the style settings of this kind of chart and hope you can help me.
R code:
set_theme(theme_bw)
cglac$pred2<-as.factor(cglac$pred)
ggplot(cglac, aes(x=depth, colour=pred2))
+ geom_bar(aes(y=..density..),binwidth=3, alpha=.5, position="stack")
+ geom_density(alpha=.2)
+ xlab("Depth (m)")
+ ylab("Counts & Density")
+ coord_flip()
+ scale_x_reverse()
+ theme_bw()
which produces this graph:
Here some points:
What I want is to have the density line as black and white lines separated by symbols rather than colour (dashed line, dotted line etc).
The other thing is the histogram itself. How do I get rid of the grey background in the bars?
Can I change the bars also to black and white symbol lines (shaded etc)? So that they would match the density lines?
Last but not least I want to add a second x or in this case y axis, because of flip_coord(). The one I see right now is for the density. The other one I need would then be the count data from the pred2 variable.
Thanks for helping.
Best,
Moritz
Have different line types: inside aes(), put linetype = pred2. To make the line color black, inside geom_density, add an argument color = "black".
The "background" of the bars is called "fill". Inside geom_bar, you can set fill = NA for no fill. A more common approach is to fill in the bars with the colors, inside aes() specify fill = pred2. You might consider faceting by your variable, + facet_wrap(~ pred2, nrow = 1) might look very nice.
Shaded bars in ggplot? No, you can't do that easily. See the answers to this question for other options and hacks.
Second y-axis, similar to the shaded symbol lines, the ggplot creator thinks a second y-axis is a terrible design choice, so you can't do it at all easily. Here's a related question, including Hadley's point of view:
I believe plots with separate y scales (not y-scales that are transformations of each other) are fundamentally flawed.
It's definitely worth considering his point of view, and asking yourself if those design choices are really what you want.
Different linetypes for densities
Here's my built-in data version of what you're trying to do:
ggplot(mtcars, aes(x = hp,
linetype = cyl,
group = cyl,
color = cyl)) +
geom_histogram(aes(y=..density.., fill = cyl),
alpha=.5, position="stack") +
geom_density(color = "black") +
coord_flip() +
theme_bw()
And what I think you should do instead. This version uses facets instead of stacking/colors/linetypes. You seem to be aiming for black and white, which isn't a problem at all in this version.
ggplot(mtcars, aes(x = hp,
group = cyl)) +
geom_histogram(aes(y=..density..),
alpha=.5) +
geom_density() +
facet_wrap(~ cyl, nrow = 1) +
coord_flip() +
theme_bw()
This question already has answers here:
ggplot2: Adjust the symbol size in legends
(5 answers)
Closed 8 years ago.
For scatterplots with many points, one common technique is to reduce the size of the points and to make them transparent.
library(ggplot2)
ggplot(diamonds, aes(x, y, colour = cut)) +
geom_point(alpha = 0.25, size = 0.5) +
ylim(0, 12)
Unfortunately, the points in the legend are now too small and faint to see properly.
I would like a way to change the points in the legend independently of the plots in the main plot panel. It ought to be one of the setting contained in:
thm <- theme_get()
thm[grepl("legend", names(thm))]
I'm struggling to find the appropriate setting though. How do I change the point size?
You can use the function guide_legend() in package scales to achieve your effect.
This function allows you to override the aes values of the guides (legends) in your plot. In your case you want to override both alpha and size values of the colour scale.
Try this:
library(ggplot2)
library(scales)
ggplot(diamonds, aes(x, y, colour = cut)) +
geom_point(alpha = 0.25, size = 1) +
ylim(0, 12) +
guides(colour=guide_legend(override.aes=list(alpha=1, size=3)))
If you need to change formatting only in the legend you should use override.aes= and size= in guide_legend (see below). This will override size used in plot and will use new size value just for legend.
To get points in legend and lines in plot workaround would be add geom_point(size=0) to ensure that points are invisible and then in guides() set linetype=0 to remove lines and size=3 to get larger points.
ggplot(iris,aes(Petal.Width,Petal.Length,color=Species))+geom_line()+theme_bw()+
geom_point(size=0)+
guides(colour = guide_legend(override.aes = list(size=3,linetype=0)))