R - change colors in ggplot2 plot [duplicate] - r

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
ggplot custom colors palette
Hello,
I am using this question - Making a stacked area plot using ggplot2 - to make a nice stacked area plot.
The resulting grapg is nice, but I cannot find how to change the colors in ggplot graph.

you need to manual designate the colors you want to use, then use scale_fill_manual.
rhg_cols1<- c("#000000","#F8766D","#7CAE00","#00BFC4","#C77CFF" )
ggplot()+geom_bar()+opts()+ scale_fill_manual(values = rhg_cols1)

Related

ggplot2 geom_col() does not show all items [duplicate]

This question already has answers here:
order of stacked bars in ggplot
(1 answer)
Force ggplot legend to show all categories when no values are present [duplicate]
(1 answer)
Can you put labels between horizontal bars?
(2 answers)
Closed last month.
I'm new to R and I'm trying to plot a small survey where I used a likert-scale.
I'm using
ggplot(Likert, aes(x=Anzahl, y=Frage, fill=Zustimmung)) + geom_col()
to display the data. There are probably better ways to do this and I probably should use data frames as I currently don't use them.
Now the actual Problem: The variable "Zustimung" has 5 different specifications but only 4 of them are shown and there are sorted weirdly.
I've found a few different posts online which seem to be regarding this issue but I can't understand them. Could someone please explain why this problem occures and how I can fix this?

How to display truncated error bars with ggplot? [duplicate]

This question already has answers here:
geom_bar bars not displaying when specifying ylim
(4 answers)
Closed 9 months ago.
I am trying to create a barplot using ggplot2, with the y axis starting at a value greater than zero.
Lets say I have the means and standard errors for hypothetical dataset about carrot length at three different farms:
carrots<-NULL
carrots$Mean<-c(270,250,240)
carrots$SE<-c(3,4,5)
carrots$Farm<-c("Plains","Hill","Valley")
carrots<-data.frame(carrots)
I create a basic plot:
p<-ggplot(carrots,aes(y=Mean,x=Farm)) +
geom_bar(fill="slateblue") +
geom_errorbar(aes(ymin=Mean-SE,ymax=Mean+SE), width=0)
p
This is nice, but as the scale runs from 0 to it is difficult to see the differences in length. Therefore, I would like to rescale the y axis to something like c(200,300). However, when I try to do this with:
p+scale_y_continuous('Length (mm)', limit=c(200,300))
The bars disappear, although the error bars remain.
My question is: is it possible to plot a barplot with this adjusted axis using ggplot2?
Thank you for any help or suggestions you can offer.
Try this
p + coord_cartesian(ylim=c(200,300))
Setting the limits on the coordinate system performs a visual zoom;
the data is unchanged, and we just view a small portion of the original plot.
If someone is trying to accomplish the same zoom effect for a flipped bar chart, the accepted answer won't work (even though the answer is perfect for the example in the question).
The solution for the flipped bar chart is using the argument ylim of the coord_flip function. I decided to post this answer because my bars were also "disappearing" as in the original question while I was trying to re-scale with other methods, but in my case the chart was a flipped one. This may probably help other people with the same issue.
This is the adapted code, based on the example of the question:
ggplot(carrots,aes(y=Mean,x=Farm)) +
geom_col(fill="slateblue") +
geom_errorbar(aes(ymin=Mean-SE,ymax=Mean+SE), width=0) +
coord_flip(ylim=c(200,300))
Flipped chart example

color in ggplot2 legend not matching figure [duplicate]

This question already has answers here:
Why are the colors wrong on this ggplot? [duplicate]
(2 answers)
GGPLOT Color won't work [closed]
(1 answer)
Closed 4 years ago.
I have a problem getting the legend in ggplot2 to correspond to the color in the figure. And I have extra variables in the legend. I have read many posts on similar issues but unfortunately I have not been able to apply those to this situation - successfully at least. I tried using "fill" and "scale_fill_manual()", "theme()" and several others, but to no avail yet. Hopefully I am just missing something simple here, I appreciate any suggestions!
I am making a map and my code right now is:
ggplot()+ geom_map(data=world, map=world,aes(x=long, y=lat, map_id=region),color="white", fill="#7f7f7f", size=0.05)+
geom_point(aes(x=long.a,y=lat.a, size=var.p, color="red", alpha=0.5))
The plot looks like this:

Border color of plot points - R plot [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to create a plot with customized points in R?
I know in R you can use plot to specify the color of the points
plot(......,col="red") makes the points in a scatter plot, for example, red.
How can I specify the border color of the points such that the border color is different from the fill color.
You can use the bg parameter to handle how a point is filled. On certain types of plotting characters, this will be visible. For example...
plot(1,1,col=3, bg=2, pch=23)

Rescaling the y axis in bar plot causes bars to disappear : R ggplot2 [duplicate]

This question already has answers here:
geom_bar bars not displaying when specifying ylim
(4 answers)
Closed 9 months ago.
I am trying to create a barplot using ggplot2, with the y axis starting at a value greater than zero.
Lets say I have the means and standard errors for hypothetical dataset about carrot length at three different farms:
carrots<-NULL
carrots$Mean<-c(270,250,240)
carrots$SE<-c(3,4,5)
carrots$Farm<-c("Plains","Hill","Valley")
carrots<-data.frame(carrots)
I create a basic plot:
p<-ggplot(carrots,aes(y=Mean,x=Farm)) +
geom_bar(fill="slateblue") +
geom_errorbar(aes(ymin=Mean-SE,ymax=Mean+SE), width=0)
p
This is nice, but as the scale runs from 0 to it is difficult to see the differences in length. Therefore, I would like to rescale the y axis to something like c(200,300). However, when I try to do this with:
p+scale_y_continuous('Length (mm)', limit=c(200,300))
The bars disappear, although the error bars remain.
My question is: is it possible to plot a barplot with this adjusted axis using ggplot2?
Thank you for any help or suggestions you can offer.
Try this
p + coord_cartesian(ylim=c(200,300))
Setting the limits on the coordinate system performs a visual zoom;
the data is unchanged, and we just view a small portion of the original plot.
If someone is trying to accomplish the same zoom effect for a flipped bar chart, the accepted answer won't work (even though the answer is perfect for the example in the question).
The solution for the flipped bar chart is using the argument ylim of the coord_flip function. I decided to post this answer because my bars were also "disappearing" as in the original question while I was trying to re-scale with other methods, but in my case the chart was a flipped one. This may probably help other people with the same issue.
This is the adapted code, based on the example of the question:
ggplot(carrots,aes(y=Mean,x=Farm)) +
geom_col(fill="slateblue") +
geom_errorbar(aes(ymin=Mean-SE,ymax=Mean+SE), width=0) +
coord_flip(ylim=c(200,300))
Flipped chart example

Resources