When using a histogram with x as a POSIXct value, I'm not sure how you're supposed to line the ticks up with the binsize of the graph.
Setting the tick size to the same as the binsize makes it line a bit off, but the offset adds onto each other until its no longer accurate.
bymonth <- ggplot() +
scale_x_datetime("", breaks = date_breaks("60 days"), labels = date_format("%m-%y")) +
...
lots of geom_rects for background colors
...
theme(legend.title = element_blank()) +
geom_histogram(data=dat, aes(x = iso, fill = name), binwidth = 30*24*60*60, position = 'dodge')
I tried using annotate() as well as experimenting with the spacing of the tick but I think my approach here might be wrong in its own accord
This leads to a graph looking something like this
Which is quite annoying
Related
I am making several instances of a tilted bar chart. As the sizes of count and the differences in percent vary, part of one of the labels (count) is pushed outside the bar in some instances. I need the labels to be entirely inside the bar in all instances. If not repositioned to fit inside the bar, I need the labels to be centered as is.
The code is:
library(tidyverse)
library(ggplot2)
data <- tibble(type = c('Cat', 'Dog'),
group = c('Pets', 'Pets'),
count = c(10000, 990000),
percent = c(1, 99))
ggplot(data, aes(x = group, y = percent, fill = type)) +
geom_bar(stat = 'identity',
position = position_stack(reverse = TRUE)) +
coord_flip() +
geom_text(aes(label = count),
position = position_stack(vjust = 0.5,
reverse = TRUE))
Use hjust="inward":
ggplot(data, aes(x = group, y = percent, fill = type)) +
geom_bar(stat = 'identity', position = position_stack(reverse = TRUE)) +
coord_flip() +
geom_text(aes(label = count), hjust = "inward", position = position_stack(vjust = 0.5, reverse = TRUE))
One thing key to note here is that plots in ggplot are drawn differently depending on the graphics device resolution, width, and height settings. This is why plots look a bit different depending on the computer you use to plot them. If I take your default graph and save different aspect ratios, this becomes evident:
width=3, height=5
width=7, height=5
The aspect ratio and resolution change the plot. You can also see this for yourself within R studio by just resizing the plot viewer window.
With that being said, there are some options to adjust your plot to be less likely to clip text out of bounds:
Rotate your text or rotate your plot back to horizontal bars. For long text labels, they are going to work out better with horizontal bars anyway.
geom_text_repel from the ggrepel package. Direct replacement of geom_text puts your labels in the plot area, and you can use min.segment.length= to specify the minimum line length as well as force= and direction= to play with positioning. Again, works better if you flip back your chart.
Use the expand= argument applied to scale_y_continuous. Try adding scale_y_continuous(expand=c(0.25,0.25)) to your plot, for example. Note that since your coordinate system is flipped, you have to specify "y" to expand "x". This expands the plot area around the geoms.
Change the output width= and height= and resolution when exporting your plots. As indicated above, this is the simple solution.
There are probably other suggestions, but that's mine.
I made a stacked barplot in ggplot2 in R:
ggplot(Count_dataframe_melt, aes(x = as.factor(variable), y = value, fill = fill)) +
geom_bar(stat = "identity",position="fill")+ scale_y_continuous(name = "Y-axis",labels = scales::percent)
I want to just visualize the top portion of the stacked barplot like so:
I've looked everywhere and can't figure out how to do this. Does anyone know how?
You can use coord_cartesian to "zoom in" on the area you want.
# your plot code...
ggplot(Count_dataframe_melt, aes(x = as.factor(variable), y = value, fill = fill)) +
geom_bar(stat = "identity",position="fill") +
scale_y_continuous(name = "Y-axis",labels = scales::percent) +
# set axis limits in coord_cartesian
coord_cartesian(ylim = c(0.75, 1))
Note that many people consider bar plots that don't start at 0 misleading. A line plot may be a better way to visualize this data.
Since the areas you want to show are less than 20% of the total area, you could flip the bar charts so that you only show the colors areas. Then the y-axis goes from 0-25% and you can use the figure caption to describe that the remaining data is in the gray category.
I'm attempting to write some code that can be used to make boxplots of temperatures at which proteins melt at, I'm 99% there except I need to introduce a line break on the y-axis of my boxplot.
Essentially, my current y axis scale goes from 45-60, I want to make the y axis start at 0, line break, 45-60. See the picture as an e.g.
I've tried using the scale_y_continuous to set a break but that didn't work as I'd hoped.
df %>%
group_by(Protein) %>%
ggplot(., aes(x = factor(Protein), y = Melting_Temperature)) +
geom_boxplot() +
theme_classic() +
geom_point(aes(x = as.numeric(df$Protein) + 0.5, colour = Protein),
alpha=0.7)+
xlab("Protein Type")+
ylab("Melting Temperature") +
stat_summary(fun.y=mean, colour = "darkred", geom = "point", shape =
18, size = 3, show_guide = FALSE) +
geom_text(data = means, aes(label = round(Melting_Temperature, 1), y =
Melting_Temperature + 0.5))
IMHO, tick marks and axis labels should be sufficient to indicate the range of data on display. So, there is no need to start an axis at 0 (except for bar charts and alike).
However, the package ggthemes offers Tufte style axes which might be an alternative to the solution the OP is asking for:
library(ggplot2)
library(ggthemes)
ggplot(iris) +
aes(x = Species, y = Sepal.Length) +
geom_boxplot() +
geom_rangeframe() +
theme_tufte(base_family = "")
Note that the iris dataset is used here in place of OP's data which are not available.
geom_rangeframe() plots axis lines which extend to the maximum and minimum of the plotted data. As the plot area is usually somewhat larger this creates a kind of gap.
theme_tufte() is a theme based on Chapter 6 "Data-Ink Maximization and Graphical Design" of Edward Tufte's The Visual Display of Quantitative Information with no border, no axis lines, and no grids.
This is not supported in ggplot as built. In this discussion from 2010, Hadley Wickham (author of ggplot as well as RStudio et al) explains that axis breaks are questionable practice in his view.
Those comments by Hadley are linked, and other options discussed, in this prior SO discussion.
Is it possible to specify distinct labels axes in each panel within ggplot?
For example:
ggplot(diamonds, aes(x = carat, y = price)) + geom_point() + facet_grid(~cut)
In this figure we have five panels, I would like to specify my own label for each of them. The default output is to produce one label for all the axes.
Is there a solution that doesn't involve using grid.arrange as is done here:
Modify x-axis labels in each facet
(I'm assuming you're referring to axis titles, not labels.)
Out of principle, no. The philosophy behind facets is that they share common aesthetic mappings. But we can trick ggplot to get what we want, for example:
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point() +
facet_grid(~cut, switch = 'x') +
theme(axis.title.x = element_blank(),
strip.background = element_blank())
The trick is to switch the facet strips to the bottom of the plot. Then we turn off the strip background and the original x-axis title to create the appearance of separate axis titles.
(You may also want to change strip.text.x = element_text(size = ??) to the same size as the y-axis title. However, it seems to not be documented what the defualt size is for axis titles.)
I have a large number of plots computed with ggplot, however when the y-axis has different number of digits, the left side of the plot are not aligned. They will not be inserted directly under / over each other, so a grid cannot be used. Nevertheless, I would like them to have the exact same size. How could this be achieved?
qplot(rnorm(10),1:10, colour = runif(10))
qplot(rnorm(10),1001:1010, colour = runif(10))
You can manually adjust the y-axis labels to match lengths or just rotate everything 90 degrees. Although there might be better solution out there.
ggplot(data.frame(x=rnorm(10),y=1:10),aes(x,y, colour = x))+geom_point()+
scale_y_continuous(breaks = seq(0,10,by=2),labels=c('0.000','2.000','4.000','6.000','8.000','10.000'))
ggplot(data.frame(x=rnorm(10),y=1001:1010),aes(x,y,colour = x) )+geom_point()+
theme(axis.text.y = element_text(angle = 90))
So would you like to have a fixed y-axis limits? you can use the coord_cartesian()
qplot(rnorm(10),1:10, colour = runif(10)) +
coord_cartesian(ylim = c(min(y_var), max(y_var)))
This shall fix the y-axis limits for all plots. Here y_var refers to the y variable being used for the y-axis