I have a faceted bar graph.
dat <- data.frame(ID = c("A", "A", "B", "B", "C", "C"),
A = c("Type 1", "Type 2", "Type 1", "Type 2", "Type 1", "Type 2"),
B = c(1, 2, 53, 87, 200, 250))
ggplot(data = dat, aes(x = A, y = B)) +
geom_bar(stat = "identity") +
facet_wrap(~ID, scales= "free_y")
How do I code to only have 3 y-axis values displayed per graph?
I've tried
+scale_y_continuous(breaks=3)
To get more control over the breaks you can write your own breaks function. The following code gives you exactly 3 breaks. However, this very basic approach does not necessarily result in "pretty" breaks:
library(ggplot2)
my_breaks <- function(x) {
seq(0, round(max(x), 0), length.out = 3)
}
my_limits <- function(x) {
c(x[1], ceiling(x[2]))
}
# Dataset 2
dat <- data.frame(
ID = c("A", "A", "A","B", "B", "B", "C", "C", "C"),
A = c("Type 1", "Type 2", "Type 3", "Type 1", "Type 2", "Type 3","Type 1", "Type 2", "Type 3"),
B = c(1.7388, 4.2059, .7751, .9489, 2.23405, .666589, 0.024459, 1.76190, 0.066678))
ggplot(data = dat, aes(x = A, y = B)) +
geom_bar(stat = "identity") +
facet_wrap(~ID, scales= "free_y") +
scale_y_continuous(breaks = my_breaks, limits = my_limits)
# Dataset 1
dat <- data.frame(ID = c("A", "A", "B", "B", "C", "C"),
A = c("Type 1", "Type 2", "Type 1", "Type 2", "Type 1", "Type 2"),
B = c(1, 2, 53, 87, 200, 250))
ggplot(data = dat, aes(x = A, y = B)) +
geom_bar(stat = "identity") +
facet_wrap(~ID, scales= "free_y") +
scale_y_continuous(breaks = my_breaks, limits = my_limits)
Created on 2020-04-10 by the reprex package (v0.3.0)
Related
I want to use the below in rmarkdown word document but the bars are too long; the axis break is good. I want to compress the size of ggplot bars while maintaining readiblity
library(tidyverse)
library(scales)
library(patchwork)
options(scipen = 999)
df <- tribble(
~category, ~ numbers, ~value,
"category 1",8, 9901020,
"category 2",8 ,18629623,
"category 3",9 ,16471680,
"category 4",7 ,13661732,
"category 5",5 ,7173011,
"category 6",10, 18395574)
dfmod <- df%>%
dplyr::mutate(numbers_lable= numbers
,numbers = numbers * 1e6)
pl <- ggplot(data = dfmod,aes(x= fct_reorder(category,desc(value))
, y = value))
pl <- pl + geom_col(fill = "grey")
pl <- pl + geom_line(aes(y = numbers, group = 1), size = .8, color = "blue")
pl <- pl + geom_text(aes(label = paste0('Sales'
, scales::comma(value)
, '\n(Orders '
, numbers_lable,')'))
, vjust = -0.3
, size = 3)
pl <- pl + scale_y_continuous(name= "Sales revenue"
,labels = scales::comma_format(scale = 1e-6
,suffix = "M")
, sec.axis = sec_axis( trans= ~./1e6
, name = "Number of sales"
, breaks = c(0,2,4,6,8,10)))
pl <- pl + theme_bw()
pl <- pl + theme(axis.title.y = element_text(color = "grey")
,axis.title.y.right = element_text(color = "blue")
)
pl
tried fig.width/height, out.width/height whenever I change them they just chop the labels
in excel I would size the chart for example as 12 cm * 5 cm
and everything would be visible but with smaller space
Extend the upper limit of your y-axis, e.g.,
sec.axis = sec_axis(
trans= ~./1e6,
name = "Number of sales",
breaks = c(0, 2, 4, 6, 8, 10)),
limits = c(0, max(df$value) * 1.2)
)
Use fig.width/fig.height for output-dependent adjustment. The following looks fine for PDF output
```{r, echo=F, fig.width=8, fig.height=4}
<your figure code>
```
Following codes generate a grid of plots shown in the figure below:
library(tidyverse)
library(cowplot)
p1 <- cars %>%
ggplot(aes(x = speed, y = dist)) +
geom_point() +
theme_bw()
p2 <- cars %>%
ggplot(aes(x = speed, y = dist)) +
geom_point() +
scale_y_continuous(trans = "log2", breaks = c(2, 4, 8, 16, 32, 64, 128),
limits = c(2, 128)) +
annotation_logticks(sides = "l") +
theme_bw()
p3 <- diamonds %>%
ggplot(aes(x = carat, y = price, colour = clarity)) +
geom_point() +
theme_bw() +
theme(legend.position = c(0.8, 0.4))
p4 <- plot_grid(p1, p1, nrow = 2, labels = c("B", "C"))
p5 <- plot_grid(p3, p4, nrow = 2, align = "hv", axis = "lb", rel_heights = c(0.5, 1),
labels = c("A", ""))
p6 <- plot_grid(p1, p2, nrow = 2, align = "h", rel_heights = c(0.5, 1),
labels = c("D", "E"))
p7 <- plot_grid(p5, p6, ncol = 2, align = "b")
p7
I wish to align the bottom axis of (A) with top axes of (B, C) respectively, and align bottom axis of (C) with the bottom axis of (E). This should stretch the (B,C) grid plot across the height of (E) and width of (A). I have tried several alignment options.
I have found queries similar to this problem which allow to either get the width or the height alignment right but not both!
I will also add that this reproducible example is the closest I can get to my actual plot. Here I have managed to align the width with panel (A) but am unable to align the height of (B,C) with (E).
May I suggest to use patchwork instead? I find that it handles the alignment of panels more elegantly than anything else around.
library(tidyverse)
library(cowplot)
p1 <- cars %>%
ggplot(aes(x = speed, y = dist)) +
geom_point() +
theme_bw()
p2 <- cars %>%
ggplot(aes(x = speed, y = dist)) +
geom_point() +
scale_y_continuous(trans = "log2", breaks = c(2, 4, 8, 16, 32, 64, 128),
limits = c(2, 128)) +
annotation_logticks(sides = "l") +
theme_bw()
p3 <- diamonds %>%
ggplot(aes(x = carat, y = price, colour = clarity)) +
geom_point() +
theme_bw() +
theme(legend.position = c(0.8, 0.4))
library(patchwork)
#>
#> Attaching package: 'patchwork'
#> The following object is masked from 'package:cowplot':
#>
#> align_plots
p3 + p1 + p1 + p1 + p2 +
plot_layout(
design = "
AD
BE
CE
"
) +
plot_annotation(tag_levels = "A")
Created on 2022-01-05 by the reprex package (v2.0.1)
So I've created a time series event. I'd like to plot my NS data on the y axes. Unfortunately it's not working and I can't figure out why. I'd like to plot it as a bar plot. The x value input should be my newly created DateTime. Any notes?
library(ggplot2)
library(scales)
Versuch3 <- data.frame(date = c("1.1.2015", "2.1.2015", "3.1.2015"),
time = c("06:12:03", "08:23:45", "15:40:32"),
NS = c("0.3", "0.4", "0.6"),
status = c("0", "1", "0"))
Versuch3$DateTime <- paste(Versuch3$date,Versuch3$time,sep =" ")
Versuch3$DateTime <- as.POSIXct(strptime(Versuch3$DateTime, "%d.%m.%Y %H:%M:%S"))
Versuch3$NS <- as.integer(Versuch3$NS)
dtLimits <- c(as.POSIXct("2014-12-01 00:00:00"), as.POSIXct("2015-07-01 00:00:00"))
#plot
ggplot(data = Versuch3, aes(x=DateTime, y=NS)) +
geom_bar(stat="identity", aes(fill=status), width = 3) +
scale_x_datetime(date_breaks = "1 month",
labels = date_format("%d.%m.%Y %H:%M:%S"),
limits = dtLimits) +
theme(axis.text.x = element_text(angle = 90, vjust = .5))
I am wondering if there is a way to add the second description of x-axis in ggplot2 as follows: Here "the second description" refers "Sample A / Sample B / two arrows" colored in red (shown in the figure).
Please click for the figure!
Of course, I can just put the "second description" using PowerPoint as I did, but I just wonder if it is possible to add it using ggplot2.
Here is the code for the background plot.
library(ggplot2)
library(ggridges)
x <- data.frame(v1=rnorm(100, mean = -2, sd = 0.022),
v2=rnorm(100, mean = -1, sd = 0.022),
v3=rnorm(100, mean = 0, sd = 0.022),
v4=rnorm(100, mean = 1, sd = 0.022),
v5=rnorm(100, mean = 2, sd = 0.022),
v6=rnorm(100, mean = 3, sd = 0.022),
v7=rnorm(100, mean = 4, sd = 0.022))
colnames(x) <- c("A",
"B",
"C",
"D",
"E",
"F",
"G")
head(x)
# Manipulate the data
library(reshape2)
data <- melt(x)
head(data)
# Generating plot
colors <- rainbow(7)
ggplot(data, aes(x = value, y = variable)) +
geom_density_ridges(aes(fill = variable), alpha=0.6, bandwidth=0.1) +
scale_fill_manual(values = colors)+
theme(axis.title = element_text(size = 12),
axis.text = element_text(size = 10),
legend.text = element_text(size = 12),
plot.title = element_text(size = 17, face = "bold",
margin = margin(b=10), hjust = 0.5),
panel.spacing = unit(0.1, "lines"),
legend.position="none") +
geom_vline(xintercept = 0, linetype="dotted") +
geom_vline(xintercept = 2, linetype="dotted",
color = "red", size=1.2) +
xlab("") +
ylab("Groups") +
labs(title = 'Density plot of each group')
Thank you in advance!
I'm not 100% sure this is what you mean, but you can add text on the x-axis using the following in labs:
labs(x="← Sample A Sample B →")
I got the arrows from unicode here: http://xahlee.info/comp/unicode_arrows.html
There are bigger arrows in the link if needed.
EDIT:
Here's your code adapted with the new labels in red font:
ggplot(data, aes(x = value, y = variable)) +
geom_density_ridges(aes(fill = variable), alpha=0.6, bandwidth=0.1) +
scale_fill_manual(values = colors)+
theme(axis.title = element_text(size = 12),
axis.text = element_text(size = 10),
legend.text = element_text(size = 12),
plot.title = element_text(size = 17, face = "bold",
margin = margin(b=10), hjust = 0.5),
panel.spacing = unit(0.1, "lines"),
legend.position="none") +
geom_vline(xintercept = 0, linetype="dotted") +
geom_vline(xintercept = 2, linetype="dotted",
color = "red", size=1.2) +
xlab("🢀 Sample A Sample B 🢂") +
theme(axis.title.x = element_text(size=40,colour = "red")) +
ylab("Groups") +
labs(title = 'Density plot of each group')
You can also push the labels further apart by adding extra spaces. Bring them closer together with fewer spaces.
I'm attempting to set up a simple R function using SQL stored procedure but I can't get it to work without errors.
Here is my script
CREATE PROCEDURE sp_aggregate (#paramGroup nvarchar(40))
AS
EXECUTE sp_execute_external_script
#language =N'R',
#script=N'
library(dplyr)
data <- data.frame(x = c(1,2,3,4,5,6,7,8,9,10), Country = c("A", "A","A","A","A",
"B", "B", "B", "B", "B"), Class = c("C", "C", "C", "C", "C", "D", "D", "D", "D", "D"))
agg_function <- function(df, column) {
enq_column <- enquo(column)
output <- df %>% group_by(!!enq_column) %>% summarize(total = sum(x))
return(output)
}
OutputDataSet <- as.data.frame(agg_function(df = data, column = paramgroup_var))
'
, #input_data_1 = N''
-- , #input_data_1_name = N'SQL_input'
, #output_data_1_name = N'OutputDataSet'
, #params = N'#paramgroup_var nvarchar(40)'
, #paramgroup_var = #paramGroup;
GO
Execute sp_aggregate #paramGroup = Country
This is the error I'm running into:
Error in grouped_df_impl(data, unname(vars), drop) :
Column `paramgroup_var` is unknown
Calls: source ... group_by.data.frame -> grouped_df -> grouped_df_impl
Error in execution. Check the output for more information.
Error in eval(ei, envir) :
Error in execution. Check the output for more information.
Calls: runScriptFile -> source -> withVisible -> eval -> eval -> .Call
Execution halted
The error is from missing a ) at the end of the data.frame()
CREATE PROCEDURE sp_aggregate (#paramGroup nvarchar(40))
AS
EXECUTE sp_execute_external_script
#language =N'R',
#script=N'
library(dplyr)
data <- data.frame(x = c(1,2,3,4,5,6,7,8,9,10),
Country = c("A", "A","A","A","A", "B", "B", "B", "B", "B"),
Class = c("C", "C", "C", "C", "C", "D", "D", "D", "D", "D"))
agg_function <- function(df, column) {
enq_column <- enquo(column)
output <- df %>% group_by(!!enq_column) %>% summarize(total = sum(x))
return(output)
}
OutputDataSet <- as.data.frame(agg_function(df = data, column = paramgroup_var))
'
, #input_data_1 = N''
-- , #input_data_1_name = N'SQL_input'
, #output_data_1_name = N'OutputDataSet'
, #params = N'#paramgroup_var nvarchar(40)'
, #paramgroup_var = #paramGroup;
GO