RStudio: ggplot default color change [duplicate] - r

This question already has answers here:
how to change the color in geom_point or lines in ggplot [duplicate]
(2 answers)
ggplot2: How to specify multiple fill colors for points that are connected by lines of different colors
(1 answer)
Closed 2 years ago.
I am trying to create a bubble chart using R studio, my codes are as follow:
library(ggplot2)
bone$gr = bone$Line
ggplot(bone, aes(x = Year, y = bup, size = Patients, col = gr)) + geom_point() +
geom_abline(intercept = -1.433254613, slope = 0.000836604, col = "red") +
geom_abline(intercept = -2.384481355, slope = 0.001309739, col = "blue")
gr is a group/class variable that divides the dataset into two groups, I want to show the two groups with different colors as this picture shows:
Is there any way I can change the color of the two groups?

Related

Create Plot with 2 Axes in R [duplicate]

This question already has answers here:
Dual y axis (second axis) use in ggplot2
(1 answer)
How can I add second axis labels in ggplot2?
(2 answers)
ggplot with 2 y axes on each side and different scales
(18 answers)
Closed 3 months ago.
I need to plot MeanDailySteps (range 0 to 30000) and MeanDailySleepHours (0-600)on two different Y axis of the same chart with the column data ID (a unique string of discrete Characters which is common to both the Y axis variables) on the X axis. The data is in a dataframe "Steps_Sleep_Data".
I tried using the ggplot and scale_y_continuous as below, unfortunately this doesn't work. I tried other means using par(new = TRUE) which didn't work. I seem to be wrong somewhere.
Please help.
ggplot(data=Steps_Sleep_Data, aes(x=Id, y=MeanDailySteps, fill=Id,colour="red", label=MeanDailySteps))+
geom_bar(stat='identity')+
ggplot(data=Steps_Sleep_Data, aes(x=Id, y=MeanDailySleepMinutes, fill=Id, colour="blue", label=MeanDailySleepMinutes)) +
geom_bar(stat='identity')+
scale_y_continuous(sec.axis = ~.*500)+
theme(axis.text.y.left = element_text(color = "red"),
axis.text.y.right = element_text(color = "blue"))

ggplot2 geom_line colors by group but with different colorcodes [duplicate]

This question already has answers here:
How to set multiple legends / scales for the same aesthetic in ggplot2?
(2 answers)
Manually setting group colors for ggplot2
(1 answer)
Changing line colors with ggplot()
(2 answers)
Closed 10 months ago.
does anyone know how I can plot several line graphs in one graph with different color codes?
ggplot(df, aes(x=variable1)) +
geom_line(aes(y=variable2,color=group1))+
geom_line(aes(y=variable3,color=group1))
I would like to have one color code for the first geom_line and a different one for the second geom_line.
color_group <- c("blue","black","yellow2","orange")
color_flag <- c("green","red","yellow2","cyan")
With
scale_colour_manual(values=color_group)
I can only assign a color code to both of them simultaneously and not separately. Thanks for your help!
You could use the ggnewscale package
library(ggnewscale)
ggplot(df, aes(x = variable1)) +
geom_line(aes(y = variable2, color = group1)) +
scale_colour_manual(values = color_group) +
new_scale_color() +
geom_line(aes(y = variable3, color = group1)) +
scale_colour_manual(values = color_flag)

How to change default color of points in ggplot2 conditional aes? [duplicate]

This question already has answers here:
how to change the color in geom_point or lines in ggplot [duplicate]
(2 answers)
Closed 4 years ago.
I want to change the default colors (currently blue and red) of the points in ggplot2 to another color set.
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color=displ<5))
ggplot(data=mpg) +
geom_point(mapping = aes(x=displ, y=hwy, color=displ<5)) +
scale_colour_manual(values=c("gold", "red"))
Here, we use a logical vector, which is converted to numeric to provide a two-color scale i.e. the value for aes(color) is as.integer(mpg$displ < 5).
We then convert this into another two-color scale of our choice, here using named values for colors. This gives:
For more choices for colors, see the following (in package:grDevices, so should be loaded by default):
demo("colors")

Overlay percentage for barplot, while keeping count on the y axis [duplicate]

This question already has an answer here:
Adding Percentages to a Grouped Barchart Columns in GGplot2
(1 answer)
Closed 5 years ago.
I have a bar plot with my independent variable on the x axis (Education level), and the count of my dependent variable on the y axis (Default on credit card debt).
ggplot(cleancc, aes(x=factor(Education), fill = factor(DefaultOct05))) + geom_bar()
I'd like to keep everything as is but simply show the percentages for each break in the bar. For example, the blue part of the bar 2 is 23.7%.
As I don't have your dataset I cannot try it, but check out this option with stat_bin():
ggplot(cleancc, aes(x=factor(Education), fill = factor(DefaultOct05))) +
geom_bar() +
stat_bin(geom = "text",
aes(label = paste(round((..count..)/sum(..count..)*100), "%")),
vjust = 5)

X axis in ggplot2: factor [duplicate]

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)

Resources