R reduce chart size and remove gray background from ggplot - r

I am using ggplot to plot some data. It works fine but I'd like to control the shape of the plotted area and to remove the grey background.
This is the code I'm using right now:
ggplot(data.melted, aes(x = Year, y = value, colour = variable)) +
geom_line() +
scale_x_continuous("Year") +
scale_y_continuous("Fraction of papers") +
scale_colour_discrete("Topics")
and this is the output it produces:

ggplot(data.melted, aes(x = Year, y = value, colour = variable)) +
geom_line() +
scale_x_continuous("Year") +
scale_y_continuous("Fraction of papers") +
scale_colour_discrete("Topics") + theme(panel.background = element_blank())
To specify dimensions when saving the plot. See ?ggsave for additional options.
ggsave(p1, file = "plot.png", width = 5, height = 5)

Related

ggplot2 Squashes Labels and Data When Using: scale_x_discrete(limit = c())

So I have this graph:
But I would like to add appropriate labels to the x axis so each column has a label. For this I used scale_x_discrete(limit = as.character(2008:2017)) however doing so gives this result:
As you can see the labels are all squashed to one side and the data bars to the other.
Am I doing something wrong? or is this bug?
Here is my code:
# First image
ggplot(data_melt, aes(x = year, y = value, fill = variable)) +
geom_bar(position = "fill", stat = "identity") +
theme(legend.position = "none")
# Second image
ggplot(data_melt, aes(x = year, y = value, fill = variable)) +
geom_bar(position = "fill", stat = "identity") +
theme(legend.position = "none") +
scale_x_discrete(limit = as.character(2008:2017))
Your labeling problem is due to the variable type and it has not nothing to do with the axis scale. Try changing the variable, in the x axis, from numerical to categorical:
data_melt$year <- as.factor(data_melt$year)

Plotting multiple Pie Charts with label in one plot

I came across this question the other day and tried to re-create it for myself. ggplot, facet, piechart: placing text in the middle of pie chart slices
. My data is in a very similar format, but sadly the accepted answer did not help, hence why I am re posting.
I essentially want to create the accepted answer but with my own data, yet the issue I run into is that coord_polar does not support free scale. Using the first answer:
I tried it using the second version of the answer, with the ddplyr version, but I also do not get my desired output. Using the second answer:
Clearly none of these has the desired effect. I would prefer to create one as with size pie charts, but only showed four as an example, follows: .
This I did in excel, but with one legend, and no background grid.
Code
title<-c(1,1,2,2,3,3,4,4,5,5,6,6)
type<-c('A','B','A','B','A','B','A','B','A','B','A','B')
value<-c(0.25,0.75,0.3,0.7,0.4,0.6,0.5,0.5,0.1,0.9,0.15,0.85)
piec<-data.frame(title,type,value)
library(tidyverse)
p1<-ggplot(data = piec, aes(x = "", y = value, fill = type)) +
geom_bar(stat = "identity") +
geom_text(aes(label = value), position = position_stack(vjust = 0.5)) +
coord_polar(theta = "y")
#facet_grid(title ~ ., scales = "free")
p1
piec <- piec %>% group_by(title) %>% mutate(pos=cumsum(value)-0.5*value)
p2<-ggplot(data = piec) +
geom_bar(aes(x = "", y = value, fill = type), stat = "identity") +
geom_text(aes(x = "", y = pos, label = value)) +
coord_polar(theta = "y")
#facet_grid(Channel ~ ., scales = "free")
p2
You don't have to supply different y values for geom_text and geom_bar (use y = value for both of them). Next you have to specify position in geom_text. Finally, remove scales from facets.
library(ggplot2)
title<-c(1,1,2,2,3,3,4,4,5,5,6,6)
type<-c('A','B','A','B','A','B','A','B','A','B','A','B')
value<-c(0.25,0.75,0.3,0.7,0.4,0.6,0.5,0.5,0.1,0.9,0.15,0.85)
piec<-data.frame(title,type,value)
ggplot(piec, aes("", value, fill = type)) +
geom_bar(stat = "identity", color = "white", size = 1) +
geom_text(aes(label = paste0(value * 100, "%")),
position = position_stack(vjust = 0.5),
color = "white", size = 3) +
coord_polar(theta = "y") +
facet_wrap(~ title, ncol = 3) +
scale_fill_manual(values = c("#0048cc", "#cc8400")) +
theme_void()

Bar plot options with ggplot2

I am actually trying to do a graph with ggplot2 but I'd like to add some options (colors, legend...).
Here is my code :
ggplot(FINAL, aes(x = as.factor(gender), y=overtreatment)) +
stat_summary(fun.y="mean", geom="bar") +
facet_grid(. ~ treatment) +
theme_grey() +
xlab("Treatment") +
ylab("OT") +
scale_fill_grey() +
theme(strip.background = element_rect(colour = "black", fill = "white"))
And here the actual output.
Could you please indicate me how to change the name of 1 and 2 (without changing in it the dataframe) and how to add colours to this ?
I tried this
ggplot(FINAL, aes(x = as.factor(gender), y=overtreatment, colour=Treatment))
But it applies the color only to the outline of the figure.
To change the color of the bars you need fill = Treatment.
To change the labels on the x axis you need scale_x_discrete(labels = your_labels). See here.
So your code will look like:
ggplot(FINAL, aes(x = as.factor(gender), y=overtreatment, fill= Treatment)) +
scale_x_discrete(labels = your_labels) +
...

Add Legend and change Colour on grouped bar plot

I created a plot like this;
library("ggplot2")
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = color, y = ..prop.., group = 2)) +
scale_y_continuous(labels=scales::percent) +
facet_grid(~cut)
Now I want to add a legend for the variable "color", also I want to change the colour of the bars. The graph is exactly how I want it to be, and if possible I don't want change the structure of the dataset, just add a legend and change colours.
I could not find example that fit for this "percentage"-style graphics.
ggplot(data = diamonds, aes(x = color, y = ..prop.., group = cut)) +
geom_bar(aes(fill = factor(..x.., labels = LETTERS[seq(from = 4, to = 10 )]))) +
labs(fill = "color") +
scale_y_continuous(labels = scales::percent) +
facet_grid(~ cut)

Try to draw a line with abline?

ggplot(G, aes(x = State, y = Score, fill = State)) +
geom_bar(stat = "identity", position = "identity", width = 0.5) +
scale_y_continuous(labels = scales::comma) +
coord_flip()
This the code I am using and I am trying to add a line at the score of 236 so how to do it and how to improve the chart in general and any edit or suggestion are always welcome.
Just use:
geom_hline(yintercept = 236)
Might be worth while to reorder your y axis and use fill = Score. It would make the plot look something like this:
df %>%
ggplot(aes(reorder(State, -Assault), Assault, fill = Assault)) +
geom_col(width = 0.75, aes(fill = Assault)) +
labs(x = "State") +
geom_hline(yintercept = 200, size = 1) +
coord_flip() +
theme_classic()
You can use geom_vline() to do this. Since you have so many bars you will want to apply the vline after your geom_bar() so that it will show up on top of your bars (rather than underneath where you can only barely see it).
ggplot(G, aes(x = State, y = Score, fill = State)) +
geom_bar(stat = "identity", position = "identity", width = 0.5) +
geom_hline(yintercept=236, color="#000000", linetype="solid") +
scale_y_continuous(labels = scales::comma) +
coord_flip()
All I've done there is add the third line to your example above. ggplot2 always confuses me with horizontal and vertical especially when you do things like coord_flip(). I think I've got it correct (even though it looks wrong it's because of the flip), but if I'm mistaken and the line comes out horizontal replace that third line above with this:
geom_vline(xintercept=236, color="#000000", linetype="solid") +
and note the only two changes, which are that vline becomes hline and xintercept becomes yintercept.

Resources