I just discovered the newish ggplotly function that makes ggplot2 graphs into interactive plotly visualizations. This is great. But I also ran into an odd effect, possibly a bug.
If I use the log= argument to change the axis scales, the axis labels disappear. log="x" will cause the x axis label to disappear, log="y" will cause the y axis label to disappear, and log="xy" will cause both to disappear.
The same thing happens if I use scale_x_log10() and scale_y_log10() functions instead of the log argument.
Is there a workaround for this?
Example (y axis label is visible, x axis label disappears):
qplot(wt, mpg, data=mtcars, colour=factor(cyl), log="x")
ggplotly()
or
qplot(wt, mpg, data=mtcars, colour=factor(cyl)) + scale_x_log10()
ggplotly()
You can get axis names if you supply them as arguments to scale_... function.
qplot(wt, mpg, data=mtcars, colour=factor(cyl)) + scale_x_log10("wt")
ggplotly()
or maybe this?
ggplot(data = mtcars, aes(x = log10(wt), y=mpg, colour = factor(cyl))) +
geom_point() +
scale_x_continuous("wt, log10-scaling")
ggplotly()
Maybe this:
gg <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() + coord_trans(y="log10")
ggplotly(gg) %>% layout(yaxis = list(type="log", autorange=TRUE))
Related
If the labels in the example below were to be overlapping, how could we implement a repel option? Thanks!
means <- df %>%
group_by(cyl) %>%
summarize(across(c(wt, mpg), mean))
ggplot(df) +
aes(x=wt, y=mpg, color=cyl, shape=cyl) +
geom_point() +
geom_point(size=4, data=means) +
geom_label(aes(label=cyl), color="black", data=means) -> fig
fig
If I add the geom_label_repel() from the ggrepel package
fig + geom_label_repel()
I get the error:
geom_label_repel requires the gollowing missing aesthetics: label
You need to map label so that geom_label_repel "sees" it. It doesn't have direct view of the mapping from other geoms. Just itself and the top ggplot call. You thus have two options.
Directly within the function
geom_label_repel(mapping = aes(label = cyl))
or in the top ggplot call
ggplot(data = df, mapping = aes(label = cyl)) +
Note that you'll probably have to specify data as Vincent mentioned in the comment if you want to label the means points.
I need to plot the geom_histogram but as the plots overlap, I want to plot it linear instead of bars, i.e, the x=hp, y=count (percent).
Can anybody please help me find how to do it.
library(ggplot2)
library(scales)
ggplot(mtcars, aes(x=hp)) +
geom_histogram(binwidth=5)
I have done it by qplot but I need to do it in ggplot and the percent.
qplot(hp, data = mtcars, geom = "freqpoly", binwidth = 10)
Thanks
You can use geom_freqpoly:
library(ggplot2)
library(scales)
ggplot(mtcars, aes(x=hp, y=..count../sum(..count..))) +
geom_freqpoly(binwidth=5) +
scale_y_continuous(labels = percent_format()) +
ylab("Percent")
I'm trying to improve my skills on R language and I found a problem.
#Load the library.
library(ggplot2)
#Execute a simple code
ggplot(mtcars, aes(x = cyl, fill = am)) + geom_bar()
My main question is, what I'm doing bad, why the fill aesthetic has not been plotted
Adrian. In the way that you are using it, with geom_bar(), fill should be a factor rather than a continuous variable.
ggplot(mtcars, aes(x = cyl, fill = as.character(am))) ## as.character or as.vector transform "am"
+ geom_bar()
To ilustate the differenece in ggplot's behavior between vector and numeric, look at this plot:
ggplot(mtcars, aes(x = cyl, fill = as.character(am), color = as.character(am), alpha = am))
+ geom_bar()
I learned from a StackOverflow post how to insert a legend for a horizontal (or vertical) line, which is:
ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point()+
geom_hline(aes(lty="foo",yintercept=20),show_guide=TRUE)+
scale_linetype_manual(name="",values=2)
That gives this plot:
However, when I specify the color of the horizontal, it will also show a separate legend for the color, as if they were different lines. See:
ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point() +
geom_hline(aes(lty="foo",yintercept=20, color = "red"),show_guide=TRUE)+
scale_linetype_manual(name="",values=2)
Finally, my question is: How can I make the legend for the color of the geom_hline disappear?
Simply following hrbrmstr's directions to get the question closed (code below). #hrbrmstr, feel free to copy/paste my response if you like.
library(ggplot2)
ggplot(mtcars, aes(x = wt, y=mpg)) + geom_point() +
geom_hline(aes(lty="foo",yintercept=20), color = "red" ,show_guide=TRUE)+
scale_linetype_manual(name="",values=2)
I want to create a scatter plot with regression line, while using size aesthetics for one of attribute. I realized that the legend now have overlaid symbol for fitted line and I want to remove that, keeping only the legend for size. How can I do that?
> library(ggplot2)
> ggplot(mtcars, aes(wt, mpg, size=cyl)) + geom_point()
This much gives this picture, which is good:
Now having smooth line on top, and then this blue "line" is what i want to get rid of, or at least make all thin like the one in the plot is.
> ggplot(mtcars, aes(wt, mpg, size=cyl)) + geom_point() + stat_smooth()
Thanks!
use legend=FALSE option
ggplot(mtcars, aes(wt, mpg, size=cyl)) + geom_point() + stat_smooth(legend = FALSE)
The most recent documentation {ggplot2} version 2.2.1 uses legend.show= NA
ggplot(mtcars, aes(wt, mpg, size=cyl)) + geom_point() + stat_smooth(show.legend = F)