Problem with y axis format ( change from 1e+01) - r

I want to change the format of y axis from 1e+01.. to 0-200000 breaks( 0, 50000, 1000000, ...200000) in my log plot
p + geom_line(aes(group = state)) + facet_wrap(~ state)+
geom_point(aes(y = positive), col = "#8B1C62")+
scale_y_continuous(limits = c(0,200000)) + theme_minimal() +
scale_y_log10()
Also I get this error message
Scale for 'y' is already present. Adding another scale for 'y', which will replace the existing scale.
I don't know what to do. Thank you in advance.

Scale for 'y' is already present. Adding another scale for 'y', which will replace the existing scale.
This is not an error but a warning. scale_y_log is overwriting the parameters you define in scale_y_continuous.
To fix a log scale, you can use coord_trans and change the limits using ylim (untested solution)
p + geom_line(aes(group = state)) + facet_wrap(~ state)+
geom_point(aes(y = positive), col = "#8B1C62")+
theme_minimal() +
scale_y_continuous(breaks = seq(0, 200000, by = 50000)) +
coord_trans(y = “log10”, ylim = c(0,200000))
Edit: I had forgetten the question of axis ticks. You need to use breaks argument in scale_y_continous. By the way, you can directly set ylim in coord_trans (cf. the doc)

Related

Change axis breaks/limits of ggplot with geom_col

I am having problems with changing the axis ticks in a barplot. I am fairly new in using ggplot so the answer might be very obvious.
Here is some data (yes it is strange, but designed to mimic the original dataset I have, which I am not allowed to share):
lab='this is just a very long example text and it will be longer and longer and longer and longer and longer and longer and longer and longer and longer and end'
number=1:20
n=unlist(lapply(number,paste,value=lab))
a=round(runif(n=20,min=-48000,max=-40000))
b=round(runif(n=20,min=-48000,max=-40000))
c=round(runif(n=20,min=-48000,max=-40000))
d=data.frame(cbind(n,a,b,c))
df=pivot_longer(d,cols=c('a','b','c'))
l1=round(as.numeric(min(df$value))/1000 )*1000+1000
l2=round(as.numeric(max(df$value))/1000 )*1000-1000
lim=seq(from=l1,to=l2,by=-1000)
colScale <- scale_fill_manual(name = "n",values = c(rainbow(nrow(df)/3)))
from which I create a barplot
p1=ggplot(df, aes(name, value, fill = as.factor(n))) +
geom_col(position = "dodge",colour='black') +
#scale_y_continuous(breaks = lim , labels = as.character(lim)) +
coord_flip() +
theme_bw() +
theme(axis.text.x=element_text(angle=90),axis.title.x=element_text(face='bold')) +
theme(axis.text.y=element_text(angle=90,size=15)) +
theme(legend.title=element_blank()) +
labs(x = "",y="test") +
colScale +
guides(fill=guide_legend(ncol=1)) +
ggtitle('something') +
theme(plot.title = element_text(hjust = 0.5,size=20))
which is this
that is basically working as I wanted, but the scaling of the x-axis is very unpleasant. What I want instead is an axis, where the breaks and labels are equal to the vector 'lim'. What I understood was that it should be possible to do this by scaling the respective axis as in the commented line. But when I'm trying this I get the error 'Discrete value supplied to continuous scale'. I tried to change the scale to 'scale_y_discrete' but then the ticks disappear completely. I tried everything I could find but nothing worked, so what is wrong?
Based on the answers I changed the plot definition to:
p1=ggplot(df, aes(name, as.numeric(value), fill = as.factor(n))) +
geom_col(position = "dodge",colour='black') +
scale_y_continuous(breaks = lim , labels = as.character(lim)) +
coord_flip() +
theme_bw() +
theme(axis.text.x=element_text(angle=90),axis.title.x=element_text(face='bold')) +
theme(axis.text.y=element_text(angle=90,size=15)) +
theme(legend.title=element_blank()) +
labs(x = "",y="test") +
colScale +
guides(fill=guide_legend(ncol=1)) +
ggtitle('something') +
theme(plot.title = element_text(hjust = 0.5,size=20))
which produced this plot
now I am able to change the axis ticks, but the plot looks nothing like the first one. My goal is to keep the look, meaning showing only the top part of the bars.
I'd suggest converting value to as.numeric (preferably before ggplot, but you can do it within, like below) and using coord_cartesian to specify the "view window". You also might find it simpler to specify your axes in the order you want them, rather than using coord_flip, which is mostly unnecessary since ggplot 3.3.0.
ggplot(df, aes(as.numeric(value), name, fill = as.factor(n))) +
geom_col(position = "dodge",colour='black') +
scale_x_continuous(breaks = lim , labels = as.character(lim)) +
coord_cartesian(xlim = c(min(as.numeric(df$value)), max(as.numeric(df$value))))
# Theming after this up to you

Set Coord_cartesian for just one side?

Is there a way to use coord_cartesian to set the lower y limit to 0, but continue the automatic setting for ymax?
I have a large set with multiple groups and categories. I want them each group to display on it's own page with facets for every category, so I am content with the automatic upper-bounds.
I am making qq plots, and because of skewed data, the normal distribution (from stat_qq_line) goes into the negative. Simply setting ymin would limit stat_qq_line, so I want to use coord_cartesian instead.
I was hoping
coord_cartesian(ylim = c(0, NA))
would work, but it produces
Error in if (zero_range(range)) zero_width else diff(range) :
missing value where TRUE/FALSE needed
Entire block if that helps:
p <- ggplot(dsub2, mapping = aes(sample = Usual)) +
stat_qq_line() + stat_qq_point() +
facet_wrap(~Category, scales = "free", labeller=labeller(Category = labels)) +
labs(title=paste("Group", group),
x = "Theoretical Quantiles", y = "Sample Quantiles") +
theme(plot.title = element_text(hjust = 0.5)) +
coord_cartesian(ylim = c(0, NA))

How to put histogram with ggplot in the beginning of axes (0,0)?

I want to create a histogram, but I have problems with putting it in the beginning of axes (0,0). Currently it is shifted in the right which looks not good. I expected expand_limits(x = 0, y = 0) to solve this. I know it might be answered already but all solutions I've found didn't work. Thank you if you point where is the problem. Here is my code:
ggplot(data=dataset, aes(x= dataset$count)) +
geom_histogram(binwidth = 3,
col="blue",
fill="darkblue") +
labs(title="Retweets Distribution") +
labs(x="Retweet number") +
theme(plot.title = element_text(hjust = 0.5)) +
scale_x_continuous(limits = c(0,250)) +
scale_y_continuous(limits = c(0,250)) + expand_limits(x = 0, y = 0)
And the plot:
Also the summary of count column:
Plots will automatically have padding between the edge of your plot area. So even if you set the axes to start at 0, you will have space between the plot area and the margins.
As you have not provided a dataset, here is a reproducible example on how to fix it. You can change the expand option WITHIN the scale_x_continuous to remove this padding:
ggplot(diamonds, aes(carat)) +
geom_histogram() +
scale_x_continuous(expand = c(0,0))
In your case, you will have to use scale_x_continuous(limits = c(0,250), expand=c(0,0))
If you then wish to shift the whole graph left, simply alter the limits.
E.g.
scale_x_continuous(limits = c(20,250), expand=c(0,0))
See the package documentation for more details: http://ggplot2.tidyverse.org/reference/scale_continuous.html

Q: R ggplot2 - Different precision on seperate tick marks

I have been working on creating a histogram of some data I that I have recent generated and in a effort to make the data more readable would like to include the confidence intervals, including having the intervals numerically marked on the tick line.
This has created a small problem with the readability. Using the code below you can see that having mean as a float value will cause all of the tick marks to have the same precision as the mean value leading to a large number of trailing 0's, in this case there are 7 but if you manully set the mean value to something like 3.5 all will have 1 trailing 0.
I was wondering if anyone knows how to set the percision of each mark manually. Ideally I would like to have the marks at 0,1,2,..,10 to be integer while the mean value would have 2 digits of precision shown since I will have a more accurate number listed.
require(ggplot2)
set.seed(1235)
df <- data.frame(x=rexp(1000))
mean = mean(df$x)
ggplot(df, aes(x=x)) +
geom_histogram(binwidth = .05, position="dodge", color="black", fill="transparent") +
geom_vline(data=df, aes(xintercept=mean), linetype="dashed", color="red") +
theme_bw() +
scale_x_continuous(name="Values", expand = c(0, 0), breaks = sort(c(seq(0,10,1), mean)))
You can set the labels parameter of scale_x_continuous. The values still overlap, so adjust accordingly or put the label elsewhere, e.g. with geom_text.
ggplot(df, aes(x = x)) +
geom_histogram(binwidth = .05, position = "dodge", color = "black", fill = "transparent") +
geom_vline(aes(xintercept = mean), linetype = "dashed", color = "red") +
theme_bw() +
scale_x_continuous(name="Values", expand = c(0, 0),
breaks = sort(c(seq(0,10,1), mean)),
labels = sort(c(0L:10L, round(mean, digits = 2))))

Add axis ticks to ggplot2

This is my code :
ggplot(dat,aes(x=x,y=y))
geom_point(size=5)
stat_smooth(method='glm', se = FALSE)
scale_y_log10()+ xlab("time")
ylab(expression(log(z^2)))
scale_y_continuous(breaks = round(seq(min(dat$y), max(dat$y), by = 10),1))
In the last line I wand to add ticks on y axis, but I have an error
Scale for 'y' is already present. Adding another scale for 'y', which will replace the existing scale.
What's wrong?
You are already doing scale_y_log10(), so adding scale_y_continuous() doesn't make sense. Instead make the breaks inside scale_y_log10():
ggplot(dat,aes(x=x,y=y)) +
geom_point(size=5) +
stat_smooth(method='glm', se = FALSE) +
scale_y_log10(breaks = round(seq(min(dat$y), max(dat$y), by = 10),1))+ xlab("time") +
ylab(expression(log(z^2)))
should work, although when you don't post a minimal example, it's hard to be sure.

Resources