extracting hex colors form jpeg, store in data.frame, and then plotting with ggplot
Like the title says. I have an image that I want to extract the hex values for the color, store in a data.frame and then plot using ggplot.
Here's what I have:
library(ggplot2)
aws.logo = 'https://eventil.s3.amazonaws.com/uploads/group/avatar/8626/medium_highres_470196509.jpeg'
temp = tempfile()
download.file(aws.logo, temp, mode = 'wb')
## matrix of colors
y = jpeg::readJPEG(temp)
val <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 255)
aws.img <- matrix(val, dim(y)[1], dim(y)[2])
out = reshape2::melt(aws.img)
names(out) = c('col', 'row', 'value')
out$value = as.character(out$value)
ggplot(out) +
geom_point(aes(x = row, y = -col), color = out$value) + ## why do I have to flip the order of
theme(legend.position="none") ## col after reshaping?
Why are the colors different from the logo on the website?
r ggplot2 jpeg
add a comment |
Like the title says. I have an image that I want to extract the hex values for the color, store in a data.frame and then plot using ggplot.
Here's what I have:
library(ggplot2)
aws.logo = 'https://eventil.s3.amazonaws.com/uploads/group/avatar/8626/medium_highres_470196509.jpeg'
temp = tempfile()
download.file(aws.logo, temp, mode = 'wb')
## matrix of colors
y = jpeg::readJPEG(temp)
val <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 255)
aws.img <- matrix(val, dim(y)[1], dim(y)[2])
out = reshape2::melt(aws.img)
names(out) = c('col', 'row', 'value')
out$value = as.character(out$value)
ggplot(out) +
geom_point(aes(x = row, y = -col), color = out$value) + ## why do I have to flip the order of
theme(legend.position="none") ## col after reshaping?
Why are the colors different from the logo on the website?
r ggplot2 jpeg
add a comment |
Like the title says. I have an image that I want to extract the hex values for the color, store in a data.frame and then plot using ggplot.
Here's what I have:
library(ggplot2)
aws.logo = 'https://eventil.s3.amazonaws.com/uploads/group/avatar/8626/medium_highres_470196509.jpeg'
temp = tempfile()
download.file(aws.logo, temp, mode = 'wb')
## matrix of colors
y = jpeg::readJPEG(temp)
val <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 255)
aws.img <- matrix(val, dim(y)[1], dim(y)[2])
out = reshape2::melt(aws.img)
names(out) = c('col', 'row', 'value')
out$value = as.character(out$value)
ggplot(out) +
geom_point(aes(x = row, y = -col), color = out$value) + ## why do I have to flip the order of
theme(legend.position="none") ## col after reshaping?
Why are the colors different from the logo on the website?
r ggplot2 jpeg
Like the title says. I have an image that I want to extract the hex values for the color, store in a data.frame and then plot using ggplot.
Here's what I have:
library(ggplot2)
aws.logo = 'https://eventil.s3.amazonaws.com/uploads/group/avatar/8626/medium_highres_470196509.jpeg'
temp = tempfile()
download.file(aws.logo, temp, mode = 'wb')
## matrix of colors
y = jpeg::readJPEG(temp)
val <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 255)
aws.img <- matrix(val, dim(y)[1], dim(y)[2])
out = reshape2::melt(aws.img)
names(out) = c('col', 'row', 'value')
out$value = as.character(out$value)
ggplot(out) +
geom_point(aes(x = row, y = -col), color = out$value) + ## why do I have to flip the order of
theme(legend.position="none") ## col after reshaping?
Why are the colors different from the logo on the website?
r ggplot2 jpeg
r ggplot2 jpeg
asked Nov 21 '18 at 20:49
R.M.R.M.
1,0531924
1,0531924
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
the raedJPEG
will return RGB color values on the 0/1 scale, not the 0/255 scale. Use
val <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 1)
Also, it's usually better not to have any $
operations in a ggplot call. Something like this would be safer
ggplot(out) +
geom_point(aes(x = row, y = -col, color = value)) +
theme(legend.position="none") +
scale_color_identity ()
If I replaceval <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 255)
withval <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 1)
I still get those funky colors. Do I have to replacereadJPEG(temp)
with something as well?
– R.M.
Nov 21 '18 at 22:03
This was test withjpeg_0.1-8
andR-3-5-0
. Are you using something different? The above works for me with that change.
– MrFlick
Nov 21 '18 at 22:20
not sure what the issue was, works perfectly now. (R-3-4-3
andjpeg_0.1-8
)
– R.M.
Nov 26 '18 at 16:22
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420262%2fextracting-hex-colors-form-jpeg-store-in-data-frame-and-then-plotting-with-ggp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
the raedJPEG
will return RGB color values on the 0/1 scale, not the 0/255 scale. Use
val <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 1)
Also, it's usually better not to have any $
operations in a ggplot call. Something like this would be safer
ggplot(out) +
geom_point(aes(x = row, y = -col, color = value)) +
theme(legend.position="none") +
scale_color_identity ()
If I replaceval <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 255)
withval <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 1)
I still get those funky colors. Do I have to replacereadJPEG(temp)
with something as well?
– R.M.
Nov 21 '18 at 22:03
This was test withjpeg_0.1-8
andR-3-5-0
. Are you using something different? The above works for me with that change.
– MrFlick
Nov 21 '18 at 22:20
not sure what the issue was, works perfectly now. (R-3-4-3
andjpeg_0.1-8
)
– R.M.
Nov 26 '18 at 16:22
add a comment |
the raedJPEG
will return RGB color values on the 0/1 scale, not the 0/255 scale. Use
val <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 1)
Also, it's usually better not to have any $
operations in a ggplot call. Something like this would be safer
ggplot(out) +
geom_point(aes(x = row, y = -col, color = value)) +
theme(legend.position="none") +
scale_color_identity ()
If I replaceval <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 255)
withval <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 1)
I still get those funky colors. Do I have to replacereadJPEG(temp)
with something as well?
– R.M.
Nov 21 '18 at 22:03
This was test withjpeg_0.1-8
andR-3-5-0
. Are you using something different? The above works for me with that change.
– MrFlick
Nov 21 '18 at 22:20
not sure what the issue was, works perfectly now. (R-3-4-3
andjpeg_0.1-8
)
– R.M.
Nov 26 '18 at 16:22
add a comment |
the raedJPEG
will return RGB color values on the 0/1 scale, not the 0/255 scale. Use
val <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 1)
Also, it's usually better not to have any $
operations in a ggplot call. Something like this would be safer
ggplot(out) +
geom_point(aes(x = row, y = -col, color = value)) +
theme(legend.position="none") +
scale_color_identity ()
the raedJPEG
will return RGB color values on the 0/1 scale, not the 0/255 scale. Use
val <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 1)
Also, it's usually better not to have any $
operations in a ggplot call. Something like this would be safer
ggplot(out) +
geom_point(aes(x = row, y = -col, color = value)) +
theme(legend.position="none") +
scale_color_identity ()
answered Nov 21 '18 at 20:56
MrFlickMrFlick
122k11140168
122k11140168
If I replaceval <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 255)
withval <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 1)
I still get those funky colors. Do I have to replacereadJPEG(temp)
with something as well?
– R.M.
Nov 21 '18 at 22:03
This was test withjpeg_0.1-8
andR-3-5-0
. Are you using something different? The above works for me with that change.
– MrFlick
Nov 21 '18 at 22:20
not sure what the issue was, works perfectly now. (R-3-4-3
andjpeg_0.1-8
)
– R.M.
Nov 26 '18 at 16:22
add a comment |
If I replaceval <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 255)
withval <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 1)
I still get those funky colors. Do I have to replacereadJPEG(temp)
with something as well?
– R.M.
Nov 21 '18 at 22:03
This was test withjpeg_0.1-8
andR-3-5-0
. Are you using something different? The above works for me with that change.
– MrFlick
Nov 21 '18 at 22:20
not sure what the issue was, works perfectly now. (R-3-4-3
andjpeg_0.1-8
)
– R.M.
Nov 26 '18 at 16:22
If I replace
val <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 255)
with val <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 1)
I still get those funky colors. Do I have to replace readJPEG(temp)
with something as well?– R.M.
Nov 21 '18 at 22:03
If I replace
val <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 255)
with val <- rgb( y[,,1], y[,,2], y[,,3], maxColorValue = 1)
I still get those funky colors. Do I have to replace readJPEG(temp)
with something as well?– R.M.
Nov 21 '18 at 22:03
This was test with
jpeg_0.1-8
and R-3-5-0
. Are you using something different? The above works for me with that change.– MrFlick
Nov 21 '18 at 22:20
This was test with
jpeg_0.1-8
and R-3-5-0
. Are you using something different? The above works for me with that change.– MrFlick
Nov 21 '18 at 22:20
not sure what the issue was, works perfectly now. (
R-3-4-3
and jpeg_0.1-8
)– R.M.
Nov 26 '18 at 16:22
not sure what the issue was, works perfectly now. (
R-3-4-3
and jpeg_0.1-8
)– R.M.
Nov 26 '18 at 16:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420262%2fextracting-hex-colors-form-jpeg-store-in-data-frame-and-then-plotting-with-ggp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown