This question already has answers here:
Spacing of discrete axis by a categorical variable
(1 answer)
Expand Categorical x-axis in ggplot
(2 answers)
Closed 3 years ago.
I am creating a plot where i have a discrete y-axis and a continuous x-axis. I want to create the impression of grouping by moving some y-axis ticks closer together and increase the space between the groups. I tried to demonstrate it by whipping something up in paint.
ggplot(data = mpg, aes(y = trans, x = displ, group = 1)) + geom_step()
So what I'm trying to do is move the manual(mx), the auto(sx), the auto(lx) closer together (blue arrows) and increase the space between these groups (red arrows)
My idea was to create empty ticks between the groups, but ggplot is ignoring those:
brks <- mpg$trans %>% unique() %>% sort()
brks <- append(brks, "test", 2)
brks <- append(brks, "", 5)
ggplot(data = mpg, aes(y = trans, x = displ, group = 1)) + geom_step() +
scale_y_discrete(breaks = brks)
Does anybody have an idea how to achieve this? Thanks!
Related
This question already has answers here:
How do you specifically order ggplot2 x axis instead of alphabetical order? [duplicate]
(2 answers)
Order discrete x scale by frequency/value
(7 answers)
Closed 17 days ago.
I have a dataset:
data <- c('real','real','real','real','real','pred','pred','pred','pred','pred','real','real','real','real','pred','pred','pred','pred')
threshold <- c('>=1','>=2','>=3','>=4','>=101','>=1','>=2','>=3','>=4','>=101','>=1','>=2','>=3','>=4','>=1','>=2','>=3','>=4')
accuracy <- c(63.4,64.4,65.1,64.3,65.4,62.1,63.6,64.1,65.4,64.8,62.2,63.3,64.4,65.6,63.1,63.8,64.6,65.1)
types<-c('morning','morning','morning','morning','morning','morning','morning','morning','morning','morning','evening','evening','evening','evening','evening','evening','evening','evening')
df <- data.frame(data,threshold,accuracy,types)
I want to plot 'data' column as stacked barplot for morning and evening separately. So I use facet wrap. My code for plotting is:
ggplot(df, aes(x = threshold, y = accuracy)) + geom_bar(aes(fill = data), stat = "identity", color = "white",position = position_dodge(0.9))+
facet_wrap(~types) +
fill_palette("jco")
And the plot I get looks like:
However, as you can see the order of threshold got messed up. I want the order for morning to look like:
'>=1','>=2','>=3','>=4','>=101'
And the order for evening should be:
'>=1','>=2','>=3','>=4'
So I have three questions:
How can I enforce the order using my code?
2 Also for evening I shouldn't be getting '>=101' so how can I remove that from the plot.
Is there a way to make the background white but keep the grid.
And on a slightly unrelated note, can you point at a graph type that might be slightly better looking than this? I am new at visualisation so I am still learning.
Insights will be appreciated.
You may set order of threshold to reorder x axis.
Then add scales = 'free' in facet_wrap to remove >=101 in evening,
add theme_bw() to make background white.
df %>%
mutate(threshold = factor(threshold, levels = c('>=1','>=2','>=3','>=4','>=101'))) %>%
ggplot(aes(x = threshold, y = accuracy)) + geom_bar(aes(fill = data), stat = "identity", color = "white",position = position_dodge(0.9))+
facet_wrap(~types, scales = 'free') +
theme_bw() +
fill_palette("jco")
This question already has answers here:
Rotating x label text in ggplot
(1 answer)
How to rotate the axis labels in ggplot2?
(2 answers)
Closed 2 years ago.
When I create a plot and change the axes labels to something other than the default (here, e.g., I am displaying no. of observations for each factor level)
# setup
set.seed(123)
library(ggplot2)
# plot
(p <-
ggplot(mtcars, aes(as.factor(am), wt)) + geom_point() +
scale_x_discrete(labels = c("0\n(n = 19)", "1\n(n = 13)")))
and then rotate the labels, the axes labels revert to the defaults:
# modify the axes label orientation
p + scale_x_discrete(guide = guide_axis(angle = 90))
#> Scale for 'x' is already present. Adding another scale for 'x', which will
#> replace the existing scale.
Is there any way I can both rotate the labels and also preserve the custom text I had entered into those labels?
P.S. And, no, this is not a duplicate of Rotating x label text in ggplot, since I am not struggling to rotate the labels (my question already includes how to do this), but to rotate labels while preserving label text. I think that's a separate issue.
Try this:
# setup
set.seed(123)
library(ggplot2)
# plot
(p <-
ggplot(mtcars, aes(as.factor(am), wt)) + geom_point() +
scale_x_discrete(labels = c("0\n(n = 19)", "1\n(n = 13)"))+
theme(axis.text.x = element_text(angle=90)))
I'm trying to produce a histogram with ggplot's geom_histogram which colors the bars according to a gradient, and log10's them.
Here's the code:
library(ggplot2)
set.seed(1)
df <- data.frame(id=paste("ID",1:1000,sep="."),val=rnorm(1000),stringsAsFactors=F)
bins <- 10
cols <- c("darkblue","darkred")
colGradient <- colorRampPalette(cols)
cut.cols <- colGradient(bins)
df$cut <- cut(df$val,bins)
df$cut <- factor(df$cut,level=unique(df$cut))
Then,
ggplot(data=df,aes_string(x="val",y="..count..+1",fill="cut"))+
geom_histogram(show.legend=FALSE)+
scale_color_manual(values=cut.cols,labels=levels(df$cut))+
scale_fill_manual(values=cut.cols,labels=levels(df$cut))+
scale_y_log10()
gives:
whereas dropping the fill from the aesthetics:
ggplot(data=df,aes_string(x="val",y="..count..+1"))+
geom_histogram(show.legend=FALSE)+
scale_color_manual(values=cut.cols,labels=levels(cuts))+
scale_fill_manual(values=cut.cols,labels=levels(cuts))+
scale_y_log10()
gives:
Any idea why do the histogram bars differ between the two plots and to make the first one similar to the second one?
The OP is trying to produce a histogram with ggplot's geom_histogram which colors the bars according to a gradient...
The OP has already done the binning (with 10 bins) but is then calling geom_histogram() which does a binning on its own using 30 bins by default (see ?geomhistogram).
When geom_bar() is used instead together with cutinstead of val
ggplot(data = df, aes_string(x = "cut", y = "..count..+1", fill = "cut")) +
geom_bar(show.legend = FALSE) +
scale_color_manual(values = cut.cols, labels = levels(df$cut)) +
scale_fill_manual(values = cut.cols, labels = levels(df$cut)) +
scale_y_log10()
the chart becomes:
Using geom_histogram() with filled bars is less straightforward as can be seen in this and this answer to the question How to fill histogram with color gradient?
This question already has answers here:
Customise x-axis ticks
(1 answer)
Increase number of axis ticks
(6 answers)
Customizing x-axis of graph
(2 answers)
Closed 5 years ago.
I am trying to plot two scatter lines in ggplot. The x axis is integers from 1 to 10. Initially I wrote the following code:
library(ggplot2)
Answer <- c(1:10)
EM = c(0.458,0.517,0.4,0.394,0.15,0.15,0.0,0.2,0.14,0.33)
F1 = c( 0.56,0.63,0.632,0.704,0.502,0.524,0.488,0.64,0.5,0.593)
test_data <- data.frame(EM,F1,Answer)
ggplot(test_data, aes(Answer)) +
geom_line(aes(y = EM, colour = "EM")) +
geom_line(aes(y = F1, colour = "F1"))
This results in the following plot
The x axis is continuous here and printing values like 2.5,7.5. To make it factor ie 1,2,3,4,...,10 I tried putting aes(factor(Answer)) but this results in empty plot. How can I fix this?
Keep your data continuous. If you want to change the scale, do just that:
ggplot(test_data, aes(Answer)) +
geom_line(aes(y = EM, colour = "EM")) +
geom_line(aes(y = F1, colour = "F1")) +
scale_x_continuous(breaks = 1:10)
This question already has answers here:
Turning off some legends in a ggplot
(2 answers)
Closed 4 years ago.
Is it possible to remove certain items from a legend created with ggplot? I have a plot that is faceted, and point sizes provide another dimension to the plot. Since the plot is faceted I do not need to have certain legend items since it is explained by the facet titles, but the legend is still relevant for the point size.
In the plot below I would like to remove the "AREA" legend items since it is already explained by the faceting, but keep the "TOTAL_VOLUME" legend items that explain the point sizes.
Here is the code used to generate the plot:
library(data.table) # Import libraries
library(ggplot2)
library(scales)
set.seed(1234) # Set Seed
area.list <- LETTERS[seq(1:7)] # 7 Possible areas
date.list <- seq(as.Date("2014/03/01"), by="month", length=13)
# Build a random data set
data <- data.table(AREA = sample(area.list, 80, replace=TRUE),
DATE = sample(date.list, 80, replace=TRUE),
VOLUME = rnorm(n=80, mean=100000,sd=40000),
NON_CONFORMING_VOLUME = rnorm(n=80, mean=30000,sd=5000))
# Summarise data by area and date
data <- data[, list(TOTAL_VOLUME=sum(VOLUME),
TOTAL_NC_VOLUME=sum(NON_CONFORMING_VOLUME)),
by=list(AREA, DATE)]
data$PERCENT_NC <- data$TOTAL_NC_VOLUME / data$TOTAL_VOLUME * 100
p <- ggplot(data = data, aes(x = DATE,
y = PERCENT_NC,
colour = AREA)) +
geom_point(aes(size = TOTAL_VOLUME)) +
geom_line() +
facet_grid(. ~ AREA) +
theme(legend.position="bottom", axis.text.x=element_text(angle=90,hjust=1)) +
ggtitle("Percent Non-Conforming by Area by Month") +
labs(x = "Month", y = "% Non-Conforming") +
scale_size_continuous(labels = comma)
plot(p)
I tried adding show_guide=FALSE to geom_point() but that removes both TOTAL_VOLUME and AREA.
Thank you
You can set the guide for each scale in the following way:
p + guides(size = "legend", colour = "none")